在Active Admin中设置新资源的初始值

时间:2014-04-18 15:35:56

标签: activeadmin

有人知道如何在活动管理中为新资源设置初始值吗?我是否应该覆盖某些操作,为资源编写控制器或挂钩到某个现有的挂钩?

1 个答案:

答案 0 :(得分:4)

是的,您可以覆盖DataAccess#build_new_resource

  # Builds a new resource. This method uses the method_for_build provided
  # by Inherited Resources.
  #
  # @returns [ActiveRecord::Base] An un-saved active record base object
  def build_new_resource
    scoped_collection.send(method_for_build, *resource_params)
  end

覆盖执行

ActiveAdmin.register Order do
    controller do
        def build_new_resource
           r = super
           r.assign_attributes(name: "new name")
           r
        end
    end 
end