Sunday, August 7, 2011

[Rails] Re: accessing a variable from an each loop

You also have to realize that javascript executes in a browser, and
ruby/rails executes on the server. What that means is that when this
appears in a .html.erb file:

$("button").click(function () {
$("<%= "#subcomNormal_#{comment[:id]}" %>").toggle();});

rails will do the string interpolations #{..} and the <%= %>
substitutions to give you something like:

$("button").click(function () {
$("#subcomNormal_3").toggle();});

But that code executes in the browser. And $("button") finds ALL the
buttons on the page. Writing this:

<% @user.comments.each do |comment| %>

<button>Hide</button>

$("button").click(function () {....

<% end %>


...does not limit the 'button' selector to that <button> tag.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate