我正在使用make_flaggable gem创建一个名为:fav
的“喜欢”按钮。我有代码工作,但我似乎无法将其转换为Ajax,因此它会动态更新。问题可能出在我的link_to
操作或redirect_to
中,但我不知道要将其更改为什么。任何帮助表示赞赏!
events_controller.rb
def fav
@event = Event.find(params[:id])
current_user.toggle_flag(@event, :fav) #toggles the fav
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
助手/ events_helper.rb
def toggle_fav(event, user)
link_to
user.flagged?(event, :fav) ? #if the user has already flagged(favoured) the event
content_tag(:span, " ", :class => "glyphicon glyphicon-heart") : #show a full heart icon
content_tag(:span, " ", :class => "glyphicon glyphicon-heart-empty"), #show an empty heart icon
fav_event_path(event), #this updates the database and I think is the cause of the problem
:remote => true
end
视图/活动/ fav.js.erb
$('.fav-heart').html("<%= escape_javascript toggle_fav(event, current_user) %>");
views / events / index.html.erb (与fav相关的内容)
<li class="fav-heart">
<%= toggle_fav(event, current_user) %>
</li>
答案 0 :(得分:0)
从我看到的内容看起来你的link_to代码可能有问题?是个 ?这样的操作分散在3条线上,还是只是为了在SO上缩短它的可见度?
这应该是有效的:
link_to user.flagged?(event, :fav) ? content_tag(:span, " ", :class => "glyphicon glyphicon-heart") : content_tag(:span, " ", :class => "glyphicon glyphicon-heart-empty"), fav_event_path(event), :remote => true
但如果你分手了吗?多行操作我不认为它会起作用。无论如何,它是糟糕的形式,如果/那么多行应该使用if语句以便于阅读。