not*_*ere 7 ruby arrays hash ruby-on-rails strong-parameters
如何允许/列出具有非常规(不可能声明)结构的深层嵌套哈希.
例:
{"widgets" => [
{
"id" => 75432,
"conversion_goal_id" => 1331,
"options" => {"form_settings"=>{"formbuilder-bg-color"=>"rgba(255, 255, 255, 0)", "font-size"=>"14px", "form-field-depth"=>"42px"}, "linkedWidget"=>""},
"type" => "formbuilder-widget"
},
{
"id" => 75433,
"conversion_goal_id" => nil,
"options" => {"width"=>"200px", "height"=>"185px", "display"=>"block", "left"=>313, "top"=>152, "position"=>"absolute"},
"type" => "social-sharing-widget"
},
{},
]}
Run Code Online (Sandbox Code Playgroud)
所以optionsJSON/hash对象没有任何指定的结构.
它是无形的.
它可以是类似的东西
{"width"=>"200px", "height"=>"185px", "display"=>"block", "left"=>313, "top"=>152, "position"=>"absolute"}
Run Code Online (Sandbox Code Playgroud)
要么:
{"form_settings"=>{"formbuilder-bg-color"=>"rgba(255, 255, 255, 0)", "font-size"=>"14px", "form-field-depth"=>"44px"},
"linkedWidget"=>"",
"required_height"=>164,
"settings"=>
[{"field_options"=>{"include_other_option"=>true, "size"=>"large", "view_label"=>false},
"field_type"=>"text",
"label"=>"Name:",
"required"=>false,
"asterisk"=>false,
"textalign"=>"left"},
{"field_options"=>{"include_other_option"=>true, "size"=>"large", "view_label"=>false},
"field_type"=>"email",
"label"=>"Email:",
"required"=>false,
"asterisk"=>false,
"textalign"=>"left"},
{"buttonalign"=>"left",
"buttonbgcolor"=>"#ba7373",
"buttonfont"=>"Old Standard TT",
"buttonfontweight"=>"bold",
"buttonfontstyle"=>"normal",
"buttonfontsize"=>"18px",
"buttonheight"=>"46px",
"buttontxtcolor"=>"#ffffff",
"field_options"=>{"include_other_option"=>true, "size"=>"large", "view_label"=>false},
"field_type"=>"button",
"label"=>"START LIVING",
"required"=>true,
"textalign"=>"left"}]}
Run Code Online (Sandbox Code Playgroud)
小部件节点就是Array.
我没有找到任何关于如何将哈希数组中的嵌套属性列入白名单的信息.
这该怎么做?
我在文档中找到了一些我可以keys直接指定的信息,
page_params.permit({widgets: [:key1, :key2]})
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为我想在options哈希中允许所有属性/键.
此解决方案也不支持数组,但它允许嵌套对象的白名单:
params.require(:screenshot).permit(:title).tap do |whitelisted|
whitelisted[:assets_attributes ] = params[:screenshot][:assets_attributes ]
end
Run Code Online (Sandbox Code Playgroud)
那么我如何在每个元素options属性(哈希数组)中列入白名单?
回复评论:
我需要options在widget节点中允许属性内的所有内容.窗口小部件节点在widgets数组中.我仍然需要阻止其他字段,例如link_text数组中的'text_value'等 - 我不希望它们被提交.
我需要强大的参数来将使用过的参数和backlist未使用的参数列入白名单.有些参数仅存在于前端,后端不存在.如果我提交所有内容 - 那么我将有例外.