【问题标题】:How do I whitelist these Rails4 params?如何将这些 Rails4 参数列入白名单?
【发布时间】:2014-09-21 02:04:05
【问题描述】:

我在我的控制器中使用它:

def step_params  
  params.require(@type.underscore.to_sym).permit(  
  :id, :name, :note, :position, :institution_id, :protocol_id, :sequence_id,:orientation_id,  
  step_item_attributes: [:id, :note, :name, :position, :institution_id, :sequence_id, :orientation_id, :_destroy ],   
  step_list_attributes: [:id, :note, :name, :position, :institution_id, :sequence_id, :orientation_id, :_destroy ])  
end 

并在提交具有嵌套属性的表单后在服务器日志中看到:

Processing by StepsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xm6oMMJ2PLXhHfKS1RkIzG5LrCUAY6vLOF+e9XHgBE4=", "step_list"=>{"name"=>"bob bob", "note"=>"", "step_items_attributes"=>{"1411264481612"=>{"name"=>"", "orientation_id"=>"1", "sequence_id"=>"1", "note"=>"a note", "_destroy"=>"false"}}}, "commit"=>"Create Step list", "type"=>"StepList"}
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
Unpermitted parameters: step_items_attributes
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "steps" ("created_at", "institution_id", "name", "note", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-09-21 01:54:49.736556"], ["institution_id", 18], ["name", "bob bob"], ["note", ""], ["updated_at", "2014-09-21 01:54:49.736556"]]
   (37.5ms)  commit transaction
Redirected to http://localhost:3000/steps/54
Completed 302 Found in 47ms (ActiveRecord: 38.7ms)

在我看来像“未经许可的参数:step_items_attributes”

... 是问题所在。

为什么我的 permit 方法不允许 step_items_attributes 哈希?我怎么能弄清楚其他符号可能会起作用?

【问题讨论】:

    标签: forms ruby-on-rails-4 parameters sti whitelist


    【解决方案1】:

    您允许的 Items 设置为 step_item_attributes,但您试图通过带有 's' 的 step_items_attributes (items)。这就是为什么你得到Unpermitted parameters: step_items_attributes

    【讨论】:

    • FWIW,我在同一个控制器中的创建操作也遇到了麻烦。我的错误正在生成错误类的对象。当我没有仔细阅读时,它会导致另一个看起来相似的错误。修复两者可以让事情再次正常工作。谢谢。
    • 哎呀。又不行了。我必须遵循这些井井有条的说明才能理解:patshaughnessy.net/2014/6/16/…
    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 2015-05-31
    • 2017-10-26
    • 2012-03-18
    • 2020-03-31
    相关资源
    最近更新 更多