ActiveAdmin 添加当前管理员作为作者

ActiveAdmin add current admin as author

我通过 ActiveAdmin 公开了一个非常简单的 Post 模型。是否可以将 Post on create 归因于作者(当前的 ActiveAdmin 用户)?在常规控制器中,我只使用:

@post = Post.new(params[:post])
@post.author = user
@post.save

您可以在 Post 模型中使用回调,例如:

before_save :set_author
def set_author
    self.author = current_user
end

或者您可以在 ActiveAdmin 控制器中修改更新操作 http://activeadmin.info/docs/8-custom-actions.html#modifying-the-controller

在您的 application.rb 中,您应该检查这一行:

config.current_user_method = :current_user

默认情况下,我认为该行通常显示为

config.current_user_method = :current_admin_user

在那种情况下,您可能希望在控制器中使用该方法。要在 Active Admin 中创建自定义控制器,您可以在 admin/post 文件中执行类似的操作:

controller do
  def create
    @post.user = current_admin_user
     super
  end