在我的Rails项目中,我想设置
ActionController::Parameters.permit_all_parameters = true
- 我创建了文件config/initializers/permit_all_params.rb 我把这行代码放在config/initializers/permit_all_params.rb
- 然后我重启rails控制台,打印ActionController::Parameters.permit_all_parameters
值仍然显示"false"。重载并重新启动rails项目并不能解决这个问题。
谁知道为什么这个初始化文件不工作,我怎么能使它工作?
谢谢!
此选项在动作控制器加载时加载:
https://github.com/rails/rails/blob/v6.1.7.2/actionpack/lib/action_controller/railtie.rb#L28
ActiveSupport.on_load(:action_controller, run_once: true) do
ActionController::Parameters.permit_all_parameters = options.delete(:permit_all_parameters) { false }
...
https://guides.rubyonrails.org/configuring.html#config-action-controller-permit-all-parameters
# config/initializers/permit_all_params.rb
Rails.configuration.action_controller.permit_all_parameters = true
在控制台中,你需要加载ActionController::Base
来执行on_load
钩子:https://github.com/rails/rails/blob/v6.1.7.2/actionpack/lib/action_controller/base.rb L271
>> ActionController::Parameters.permit_all_parameters
=> false
# load action controller
>> ActionController::Base
=> ActionController::Base
>> ActionController::Parameters.permit_all_parameters
=> true