Rails强参数 - 允许在数组中使用深嵌套哈希

时间:2016-02-26 17:49:01

标签: ruby-on-rails arrays ruby hash strong-parameters

如何允许/列出具有非常规(不可能声明)结构的深层嵌套哈希。

示例:

addNewItemToList: function(){

  var t = this.state.items;  //my cache of list items
  var newListItem = {};
  newListItem['id'] = 123;
  t.push(newListItem);  //adding-mutating the cache

  //creating a new immutable ds
  var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
  this.setState({
    listForUI: ds.cloneWithRows(t)
  }, function(){

     //update server with change
     fetch('http://localhost:8080/accounts/1/lists/3/todos', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        //code ommitted
  }

 }

所以{"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" }, {}, ]} JSON / hash对象没有任何指定的结构。

这是无形的。

可能类似

options

OR:

{"width"=>"200px", "height"=>"185px", "display"=>"block", "left"=>313, "top"=>152, "position"=>"absolute"}

小部件节点只是 {"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"}]}

我没有找到任何关于如何将哈希数组中的嵌套属性列入白名单的信息。

怎么做?

我在文档中找到了一些我可以直接指定Array的信息,

keys

但这不会起作用,因为我想在page_params.permit({widgets: [:key1, :key2]}) 哈希中允许所有属性/键。

This solution,也不支持数组,但它允许列出嵌套对象:

options

那么我如何在每个元素params.require(:screenshot).permit(:title).tap do |whitelisted| whitelisted[:assets_attributes ] = params[:screenshot][:assets_attributes ] end 属性(哈希数组)中列入白名单?

回复评论:

  1. 我需要允许widget节点中options属性中的所有内容。窗口小部件节点位于options数组中。我仍然需要阻止其他领域,例如widgets,' text_value'数组中的其他内容 - 我不希望它们被提交。

  2. 我需要强大的参数来将使用过的参数列入白名单,并将未使用的参数列入后备列表。有些参数仅存在于前端,不存在于后端。如果我提交所有内容 - 那么我将有例外。

1 个答案:

答案 0 :(得分:-2)

也许我不遵循你在这里尝试做的事情。强大的参数是防止用户提交恶意数据,因此它基本上将某些密钥列入白名单。如果你想允许一切,那你需要什么样的强力参数?