1

我试图切换按钮而不使用 ajax 刷新

在我的 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

在 microposts/like.js.erb

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

我认为语法是混乱的。该#{@micropost.id}部分不工作。

4

1 回答 1

3

代替

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

$("#vote_form_<%=@micropost.id}%>").html("undo");
$("#unvote_form_<%=@micropost.id}%>").html("Into it!");
于 2013-01-04T11:00:16.223 回答