我在评论 Controller 中使用 Rails Inherited_resource gem,并且评论是嵌套资源,因此:
资源:项目做
资源:评论做
结束
我在评论 Controller 中还有一个belongs_to:
属于:project, :finder => :find_by_project_uuid!, :class_name => "Thfz::Project", :polymorphic => true
如何在创建评论时将其用户关联设置为 current_user(user_id)?因为 user_id 不应该被大量分配。
我尝试了以下操作:
def begin_of_association_chain
当前用户
结束
这确实正确设置了用户 ID,但我无法使用此项目获取嵌套资源。销毁评论时也会出现同样的问题,我需要通过current_user找到评论,那么如何实现这一点?
那么我必须编写自己的创建和销毁操作吗?
谢谢:)
最佳答案
您是否在comments_controller中尝试过以下操作?
class CommentsController < InheritedResources::Base
before_filter :authenticate_user! # Assuming you are using Devise for authentication
respond_to :html, :xml, :json
belongs_to :project, :finder => :find_by_project_uuid!, :class_name => "Thfz::Project"
def create
@comment = build_resource
@comment.author = current_user
create!
end
end
关于ruby-on-rails - 使用继承资源rails创建评论时设置当前用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13753283/