【发布时间】:2015-01-11 20:07:42
【问题描述】:
我有一个模型“事物”,每个模型都有_many“评论”,每个模型又都有_many“投票”。我希望能够在 Thing 显示页面上对 cmets 进行投票。这是我目前所拥有的:
评论控制器:
def votecomment
@comment = Comment.find(params[:id])
Vote.create!(voteable_id: params[:id], voteable_type: 'Comment')
redirect_to current_thing
end
事物视图:
<%= link_to “Vote”, vote_comment_path(:id => comment.id), method: :post %>
路线:
post 'comments/:id/vote' => 'comments#vote', as: 'vote_comment'
但是我得到了这个错误:
NameError in CommentsController#votecomment
undefined local variable or method `current_thing' for #<CommentsController:0x007f98efa69c00>
我尝试将方法移动到 Things 控制器,但我得到了完全相同类型的错误。
我做错了什么?
【问题讨论】:
-
试试
redirect_to @comment.thing -
你是怎么想到
current_thing的? -
你能把
current_thing的代码贴在你的控制器里 -
@Santosh 啊,效果很好!谢谢你。我在 current_user 工作的地方看到了一堆答案,所以我认为 current_thing 也可以工作。
-
很高兴它成功了。我已将其添加为答案。 :)
标签: ruby-on-rails ruby redirect ruby-on-rails-4 routes