Jaqx
Jaqx

Reputation: 826

Ajax js syntax with ruby variable

Im trying to toggle a button without refreshing using ajax

In my micropost_helper.rb

def toggle_like_button(micropost, user)
  if user.voted_for?(micropost)
    link_to "undo", like_micropost_path(micropost), :class => "btn btn-mini btn-primary", :id =>"unvote_form_#{micropost.id}", :remote => true
  else
    link_to "Into it!", like_micropost_path(micropost), :class => "btn btn-mini btn-primary", :id =>"vote_form_#{micropost.id}", :remote => true
  end
end

In microposts/like.js.erb

$("#vote_form_#{@micropost.id}").html("undo")
$("#unvote_form_#{@micropost.id}").html("Into it!")

I think the syntax is messed up. the #{@micropost.id} part is not working.

Upvotes: 1

Views: 91

Answers (1)

shweta
shweta

Reputation: 8169

replace

$("#vote_form_#{@micropost.id}").html("undo")
$("#unvote_form_#{@micropost.id}").html("Into it!")

with

$("#vote_form_<%[email protected]}%>").html("undo");
$("#unvote_form_<%[email protected]}%>").html("Into it!");

Upvotes: 3

Related Questions