导轨 3.2。我正在使用以下代码关联user_id
到记录:
# review.rb
class Review < ActiveRecord::Base
belongs_to :reviewable, :polymorphic => true, :counter_cache => true
end
# reviews_controller.rb
def create
@review = @reviewable.reviews.new(params[:review])
@review.user_id = current_user.id
if @review.save
flash[:success] = 'Thanks for adding your review.'
redirect_to @reviewable
else
flash[:error] = 'Error adding review, please try again.'
redirect_to @reviewable
end
end
我想找到一种使用它的方法,但它一直说current_user
未定义,但我可以找到current_user
对象:
def create
@review = @reviewable.current_user.reviews.create(params[:review]
if @review.save
flash[:success] = 'Thanks for adding your review.'
redirect_to @reviewable
else
flash[:error] = 'Error adding review, please try again.'
redirect_to @reviewable
end
end