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

问题描述 投票:0回答:1

我现在尝试了几个小时,才能仅“允许”控制器中的以下params-hash:

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

我的方法是使用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

输出仍然是,不允许使用参数-因此,即使哈希设置很好,我也无法使用控制器中视图的输入。

<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>

我显然在犯错误并违背常规的做法是什么?我认为使用tap是一种明智的方法,但显然不足以“破解Rails代码”。 :)

需要帮助!

ruby-on-rails strong-parameters
1个回答
0
投票

您尝试过这个吗?

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

这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.