Sah*_*hil 3 ruby-on-rails activeadmin
鉴于以下设置(目前不工作)
class Employee < ActiveRecord::Base
end
class Manager < Employee
end
ActiveAdmin.register Employee do
form do |f|
f.input :name
f.input :joining_date
f.input :salary
f.input :type, as: select, collection: Employee.descendants.map(&:name)
end
end
Run Code Online (Sandbox Code Playgroud)
我希望为所有员工提供一个"新"表单,并能够在表单中选择员工的STI类型.我能够按预期看到"类型"的选择框,但是当我点击"创建"按钮时,我收到以下错误:
ActiveModel::MassAssignmentSecurity::Error in Admin::EmployeesController#create
Can't mass-assign protected attributes: type
Run Code Online (Sandbox Code Playgroud)
现在,我知道受保护的属性在Rails中的工作方式,我有一些解决方法,如定义,Employee.attributes_protected_by_default但这会降低安全性,而且太糟糕了.
我希望能够使用ActiveAdmin中的某些功能执行此操作,但我找不到一个.我不想创建自定义控制器操作,因为我展示的示例是高度简化和设计的.
我希望以某种方式由ActiveAdmin生成的控制器识别type并执行Manager.create而不是Employee.create
有没有人知道解决方法?
您可以自己自定义控制器.阅读自定义控制器上的ActiveAdmin文档.这是一个简单的例子:
controller do
alias_method :create_user, :create
def create
# do what you need to here
# then call create_user alias
# which calls the original create
create_user
# or do the create yourself and don't
# call create_user
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5872 次 |
| 最近记录: |