本文介绍了活动管理员允许的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从活动管理员的 permit_params 块中判断我是处于创建模式还是编辑模式?我想在创建新记录时合并 created_by 和 updated_by.当我编辑现有记录时,我只想合并 updated_by.

Is there a way to tell whether I am in create or edit mode from inside the permit_params block of active admin? I'd like to merge created_by and updated_by when I'd creating a new record. When I'm editing an existing record I only want to merge updated_by.

这是我的代码:

ActiveAdmin.register Group do
  menu parent: "Groups", priority: 0

  permit_params do
    permitted = [:name, :description, :group_type_id, :owner_user_id]
    permitted.merge!(updated_by: current_admin_user)
    permitted
  end

我想说:

permitted.merge!(created_by: current_admin_user) if new_record?

新记录?是一种主动管理方法,但它似乎只能在表单中使用.

new_record? is an active admin method but it only seems to work in forms.

有什么想法吗?

推荐答案

您也可以在创建和更新操作中合并它:

You can merge it in the create and update action, too:

controller do
  def create
    params[:group].merge!(created_by: current_admin_user)
    create!
  end

  def update
    params[:group].merge!(updated_by: current_admin_user)
    update!
  end

end

这篇关于活动管理员允许的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 17:05