当用户在没有先登录的情况下尝试投票时,我收到此错误:
nil:NilClass 的未定义方法“vote_for”
我有一个常规的“帖子”脚手架,用户正在对帖子进行投票。如果他们尚未登录,如何插入将他们重定向到 user_sign_in 的命令?
class PostsController < InheritedResources::Base
def vote_up
begin
current_user.vote_for(@post = Post.find(params[:id]))
redirect_to [@post]
flash[:success] = "You have voted successfully"
rescue ActiveRecord::RecordInvalid
redirect_to [@post]
flash[:error] = "You have already voted"
end
end
end