Anders
Anders

Reputation: 2941

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?

Upvotes: 1

Views: 424

Answers (1)

Ajay
Ajay

Reputation: 4251

Instead of

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

Try:

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

Upvotes: 3

Related Questions