我正在使用 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>