列入白名单的参数不会出现在强参数中

时间:2017-04-18 15:49:58

标签: ruby-on-rails strong-parameters

我遇到以下问题,我尝试使用Google解决此问题,但它没有给出任何明显的结果。

我有更新模型的表单,我一直在尝试向强参数添加新参数( model_params ),看起来他们不包含这些参数,事件虽然它们的值出现在常规的参数中(使用 byebug 来检查它们)

def model_params
   params.require(:model).permit(
    :id, # new param
    :hidden_field_param, # also new one
     # Long list of parameters omitted
    nested_model_attributes: [:id, :file, :_destroy])
end

PARAMS

<ActionController::Parameters { All parameters including new and old ones } permitted: false>

model_params

<ActionController::Parameters { Only old ones } permitted: true>

谢谢。

UPD

_form.html.erb (部分)

<%= form_for @model, :remote => true, :authenticity_token => true, :html => { multipart: true } do |f| %>

    <!-- All other fields omitted, they are working correctly -->

    <div class="row">
        <div class="col-xs-12">
            <%= hidden_field_tag :hidden_field_param, 'Here is string value' %>
            <% count = 0 %>
            <%= f.fields_for :nested_model, method: :post, class: "" do |ff| %>
                <%= ff.file_field :file, multiple: true, id: 'pictures_' + (count=count+1).to_s, class: "image_item" %>
            <% end %>
        </div>
    </div>
    <p>
       <%= f.submit 'Done', class: 'btn btn-primary' %>
    </p>

<% end %>

1 个答案:

答案 0 :(得分:0)

请注意,您的隐藏字段标记被称为&#34; hidden_​​field&#34;而不是&#34; hidden_​​field_param&#34;。此外,您不需要提及:id保存到数据库,因为它是自动增量。你的模型参数应该是这样的:

def model_params
   params.require(:model).permit(
     :hidden_field,
     # Long list of parameters omitted
   nested_model_attributes: [:id, :file])
end