问题描述
我有一个功能,用户喜欢/不象和不喜欢/ undislike菜肴。这就是我最初有我的行为设置:
I have a feature where users like/unlike and dislike/undislike dishes. This is how I initially have my actions setup:
菜控制器
class DishesController < ApplicationController
def like
@dish.liked_by current_user
end
def dislike
@dish.disliked_by current_user
end
...
end
考虑到 js.erb
模板,每个模板是完全一致的,我想只创建一个共享的部分,每个可以使用:
Considering the js.erb
templates for each template are completely identical I wanted to create just one shared partial that each could use:
原始设置
# like.js.erb, unlike.js.erb,dislike.js.erb,undislike.js.erb
$(".restaurant__dish-vote--<%= @dish.modifier_class %>").html('<%= escape_javascript(render "restaurants/menu_partials/dish_votes", dish: @dish) %>');
新设置
# shared/_vote.js.erb
$(".restaurant__dish-vote--<%= @dish.modifier_class %>").html('<%= escape_javascript(render "restaurants/menu_partials/dish_votes", dish: @dish) %>');
现在我想修改我的控制器,以反映这些变化,但在Ajax功能似乎并不具有以下工作:
Now I'm trying to refactor my controller to reflect these changes but the ajax functionality doesn't seem to work with the following:
class DishesController < ApplicationController
def like
respond_to do |format|
format.js { render partial: "dishes/shared/vote.js.erb" }
end
@dish.liked_by current_user
end
end
的操作完成,但是,从面向用户侧的变化时刷新仅反射。我究竟做错了什么?
The action completes but the changes on the user-facing side are only reflected upon refresh. What am I doing wrong?
推荐答案
我想你应该叫 @ dish.liked_by CURRENT_USER
在渲染部分: 菜/共享/ vote.js.erb
。问题是, @dish
尚未更新。
I think you should call @dish.liked_by current_user
before render partial: "dishes/shared/vote.js.erb"
. The problem is that @dish
is not updated yet.
这篇关于使用的respond_to方法来呈现js.erb部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!