1

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.

1 Answer 1

3

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!");
Sign up to request clarification or add additional context in comments.

1 Comment

you meant $("#vote_form_<%[email protected]%>").html("undo") $("#unvote_form_<%[email protected]%>").html("Into it!") but thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.