文档 ( http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters ) 中用于强参数的示例是:
params.require(:person).permit(:name, :age)
但是,这不起作用,因为 require() 返回参数的值,而不是另一个可以调用 permit() 的参数对象。
$ rails console
1.9.3p286 :005 > params = ActionController::Parameters.new(person: 1, name: "Joe", age: 30)
=> {"person"=>1, "name"=>"Joe", "age"=>30}
1.9.3p286 :006 > params.require(:person).permit(:name, :age)
NoMethodError: undefined method `permit' for 1:Fixnum
from (irb):6
from /Users/jeremyburton/.rvm/gems/ruby-1.9.3-p286/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/jeremyburton/.rvm/gems/ruby-1.9.3-p286/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/jeremyburton/.rvm/gems/ruby-1.9.3-p286/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
此外,require 只能采用 1 个参数似乎很奇怪。看来你应该能够做到:
params.require(:a, :b).permit(:c, :d)
鉴于,您需要做的是:
params.require(:a)
params.require(:b)
params.permit(:a, :b, :c, :d)
这似乎不是很干燥 - 如果需要一个参数,那么它当然是允许的。
我错过了什么吗?