0

我正在使用 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(与收藏相关的内容)

<li class="fav-heart">
  <%= toggle_fav(event, current_user) %>
</li>
4

1 回答 1

0

From what I can see it looks like your link_to code might be at fault? Is the ? operation being spread across 3 lines like that or did you just shorten it for viewability here on SO?

This should be valid:

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

But if you are breaking up a ? operation across multiple line I don't think it will work. It's bad form anyway, multi-line if/then should use an if statement for readability.

于 2013-09-01T19:19:34.220 回答