Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  102] [ 7]  / answers: 1 / hits: 17912  / 13 Years ago, sat, may 14, 2011, 12:00:00

I have an index.html.erb file with the following:




<%= render @users %>




This renders _user.html.erb and outputs a button for performing a certain action on each user:




<%= link_to action, action_path(user), :id => #{user.id}_action, :remote => true) %>




I've set up my User controller to respond to the AJAX request by looking at action.js.erb.



In order to perform javascript methods on particular users within the partial, and I'd like to know how instance variables from my partial (such as user.id) can be passed into or accessed within the js.erb file, for instance:



$(#{@user.id}_action).toggle();

More From » jquery

 Answers
9

You access the information in your *.js.erb-files just like in your *.html.erb-files, using @users or other instance variables declared in the controller:



$(#<%= @user.id %>_action).toggle();

[#92239] Thursday, May 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;