简体   繁体   English

如何要求和允许多个强参数

[英]How to require and permit multiple strong parameters

I try for hours now to only "permit" the following params-hash in the controller:我现在尝试了几个小时,只“允许”控制器中的以下参数哈希:

{
  "utf8"=>"✓",
  "authenticity_token"=>"...",
  "article"=>{
    "title"=>"Titel Tags",
    "text"=>"Tags Tags Tags"
  },
  "tags"=>{
    "name"=>"ufos, foo, bar, aerzte"
  },
  "commit"=>"Create Article"
}

My approach is with tap我的方法是tap

def article_params
params.tap { |article_params| article_params.require(:article).permit(:title, :text)}.tap {|tags_params| tags_params.require(:tags).permit(:name) }
end

Output is still, that the parameters are not permitted - so I can't use the input from the view in my controller at all, even though the hash is set up fine.输出仍然是不允许使用参数 - 所以我根本无法使用控制器中视图的输入,即使散列设置得很好。

<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"DuMUDfPFe6iFq2Jwj4gTst1nFI3JVwTCoXu/oL53TxE1cXhtK1d+WOBL4U7A3Efo2sGxr7RCHLx3LTau7SK0xg==", "article"=><ActionController::Parameters {"title"=>"Titel Tags", "text"=>"Tags Tags Tags"} permitted: false>, "tags"=><ActionController::Parameters {"name"=>"ufos, foo, bar, aerzte"} permitted: false>, "commit"=>"Create Article", "controller"=>"articles", "action"=>"create"} permitted: false>

What is it that I am obviously doing horribly wrong and against the rails way?我显然做错了什么并且违背了轨道? I thought using tap would be darn smart an approach but obviously not smart enough for "cracking the rails code".我认为使用tap是一种非常聪明的方法,但显然不够聪明,无法“破解 Rails 代码”。 :) :)

Help needed !需要帮助 !

Have you tried this?你试过这个吗?

def article_params
  params.permit(
    :utf_8,
    :authenticity_token,
    :commit,
    tags: [:name],
    article: [:title,:text]
  )
end

This is works for me.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM