0

I'm using Rails 3.1 and trying to add some ajax functionality (using jquery + coffeescript).

I have a respond_to block in the controller

 def edit       
    @variable = 123
    respond_to do |format|
      format.js 
    end
  end

and a file app/views/test/edit.coffee.js

The file edit.coffee.js is be picked up and is running correctly, but I can't work out how to access @variable from the javascript.

Is there a way to pass this variable in to the js.coffee script? or do update an element on the page so that I can access it from the js.coffee?

2 Answers 2

2

Change the name of your view file to edit.coffee.js.erb and then reference @variable ERB-style in your CoffeeScript:

some_method: ->
    console.log('<%=j @variable %>')
Sign up to request clarification or add additional context in comments.

Comments

1

With rails 3.2.1 I used

edit.js.coffee

and inside the template

alert '<%=j @variable %>'

to make this work.

I didn't work with the .erb suffix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.