Ruby-on-rails 如何让 ActiveAdmin 使用强参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13091011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get ActiveAdmin to work with Strong Parameters?
提问by Gary S. Weaver
Update: this question was asked before there was a solution for it already in ActiveAdmin. As Joseph states, the ActiveAdmin documentation now contains this information, but the answers here are provided for those working with older versions of ActiveAdmin.
更新:这个问题是在 ActiveAdmin 中已有解决方案之前提出的。正如 Joseph 所说,ActiveAdmin 文档现在包含此信息,但这里的答案是为那些使用旧版本 ActiveAdmin 的人提供的。
When the strong_parameters 0.1.4 is used with ActiveAdmin 0.5.0 in Rails 3.2.8, if the model you are using is using StrongParameters by including:
当 strong_parameters 0.1.4 与 Rails 3.2.8 中的 ActiveAdmin 0.5.0 一起使用时,如果您使用的模型正在使用 StrongParameters,请包括:
include ::ActiveModel::ForbiddenAttributesProtection
then you get the following error in the log if you try to create/edit a record:
如果您尝试创建/编辑记录,则会在日志中收到以下错误:
ActiveModel::ForbiddenAttributes (ActiveModel::ForbiddenAttributes)
采纳答案by Joseph N.
The documentation now clearly states how to go about Setting up Strong Parameters in Rails 4. See:
该文档现在清楚地说明了如何在 Rails 4 中设置强参数。请参阅:
回答by Brendon Muir
Update to the latest inherited_resources gem and do this in your controller block:
更新到最新的 inherit_resources gem 并在您的控制器块中执行此操作:
ActiveAdmin.register Blog do
#...
controller do
#...
def permitted_params
params.permit(:blog => [:name, :description])
# params.permit! # allow all parameters
end
end
end
回答by Nick Urban
The accepted answer did not work for me with resources defined in an engine, so I tracked down the original resource_params in inherited_resources/lib/inherited_resources/base_helpers.rb and came up with this solution which closer mimics that code, and which works with engines:
对于引擎中定义的资源,接受的答案对我不起作用,所以我在inherited_resources/lib/inherited_resources/base_helpers.rb 中找到了原始的resource_params,并提出了这个解决方案,它更接近地模仿了该代码,并且适用于引擎:
In config/initializers/active_admin.rb:
在config/initializers/active_admin.rb:
ActiveAdmin::ResourceController.class_eval do
# Allow ActiveAdmin admins to freely mass-assign when using strong_parameters
def resource_params
[(params[resource_request_name] || params[resource_instance_name]).try(:permit!) || {}]
end
end
回答by Manish Kasera
in your config/initializers/active_admin.rb
在你的 config/initializers/active_admin.rb
config.before_filter do
params.permit!
end
回答by Gary S. Weaver
Update: See @Brendon-Muir's answer for latest way to do this. The following information was correct previously, so I'll leave it here in case it helps others with an older version of ActiveAdmin.
更新:请参阅@Brendon-Muir's answer了解最新的方法。以下信息以前是正确的,因此我将其保留在这里,以防它对使用旧版本 ActiveAdmin 的其他人有所帮助。
A patch had been proposed in a google group thread: https://groups.google.com/forum/?fromgroups=#!topic/activeadmin/XD3W9QNbB8I
一个补丁已在 google 群组线程中提出:https: //groups.google.com/forum/?fromgroups=#!topic/activeadmin/ XD3W9QNbB8I
Then was being put together here: https://github.com/gregbell/active_admin/issues/1731
然后被放在一起:https: //github.com/gregbell/active_admin/issues/1731
But for now, the least invasive way to add strong parameters support to ActiveAdmin in your app is to redefine resource_params in your controller block, either via the "permit all params" method, which is less secure:
但就目前而言,在您的应用程序中为 ActiveAdmin 添加强参数支持的侵入性最小的方法是在您的控制器块中重新定义 resource_params,或者通过“允许所有参数”方法,这种方法不太安全:
controller do
def resource_params
return [] if request.get?
[ params[active_admin_config.resource_class.name.underscore.to_sym].permit! ]
end
end
or the more secure explicit way:
或更安全的明确方式:
controller do
def resource_params
return [] if request.get?
[ params.require(:name_of_model).permit(:each,:param,:goes,:here,:if,:you,:want) ]
end
end
See Active Admin docs on modifying controllers:
http://activeadmin.info/docs/8-custom-actions.html#modify_the_controller
请参阅有关修改控制器的 Active Admin 文档:http:
//activeadmin.info/docs/8-custom-actions.html#modify_the_controller
回答by Davidslv
You can also use permit_paramsas follows:
您还可以permit_params按如下方式使用:
ActiveAdmin.register Resource do
permit_params do
%i(first_name last_name)
end
index pagination_total: false do
column :id
column :first_name
column :last_name
actions
end
end

