简体   繁体   中英

Rails 4 - How do I use dynamic div ids from div_for in a js.erb file?

I have a Rails app in which I have a list of items. Each item have it's own like button. When clicking this like button I perform some Ajax / Javascript to update the status. I'm having some issues with getting the correct button to update.

My like button looks like this:

div_for(idea) do
  - if current_user.voted_for? idea
    = render "ideas/unlike_button", idea: idea
  - else
    = render "ideas/like_button", idea: idea

My like.js.erb file currently looks like this:

$("#<%= idea_#{@idea.id} %>").html("<%= j render partial:  'ideas/unlike_button', locals: {idea: @idea} %>");

I don't think this is working as I want: "#<%= idea_#{@idea.id} %>"

What I want is "idea_123" .

How can I achieve that?

Instead of

$("#<%= idea_#{@idea.id} %>").html("Your code");

Try:

$("#idea_<%= @idea.id %>").html("your code");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM