不允许的参数:嵌套表单

时间:2018-12-04 11:08:17

标签: ruby-on-rails ruby forms nested

有人可以在下面检查我的代码。

我有一个带有嵌套表单的表单,并且我一直都在获取有关“ Unpermitted parameters:”的信息,如:

Unpermitted parameters: :item_custom_inputs_attributes, :item_custom_select_inputs_attributes, :item_custom_date_inputs_attributes
Unpermitted parameters: :item_custom_inputs_attributes, :item_custom_select_inputs_attributes, :item_custom_date_inputs_attributes
Unpermitted parameters: :item_custom_inputs_attributes, :item_custom_select_inputs_attributes, :item_custom_date_inputs_attributes
Unpermitted parameters: :item_custom_inputs_attributes, :item_custom_select_inputs_attributes, :item_custom_date_inputs_attributes

这也是控制器中的参数值:

<ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"XV038rd3aC43ZGw18MZlTxlho3IdsUPJlD/azWfBKdDgakbY/rmRqeNTjRv4KAczi5LMcClZA41xav5HMYOnzw==", "account_show_update_button"=>"true", "item"=><ActionController::Parameters {"name"=>"Bags & Backpacks", "found_at_date"=>"2018-11-27", "show_found_date"=>"1", "found_at_time"=>"10:33:22.000", "show_found_time"=>"1", "expiration_date"=>"2018-12-27","item_storage_location_id"=>"", "show_image"=>"0", "category_id"=>"3", "tag_list"=>"", "description"=>"", "internal_notes"=>"", "item_custom_inputs_attributes"=><ActionController::Parameters {"11"=>{"data"=>"text data eeeeeeeeeeee"}, "8"=>{"data"=>""}} permitted: false>, "item_custom_select_inputs_attributes"=>{"7"=>{"data"=>"9"}}, "item_custom_date_inputs_attributes"=>{"4"=>{"data"=>""}}} permitted: false>, "commit"=>"Update", "q"=>{"state_eq"=>"queued", "view_type"=>"list"}, "controller"=>"items", "action"=>"update", "venue_permalink"=>"asgardia", "id"=>"667"} permitted: false>

控制器允许使用以下不允许的参数之一:

def item_params
    params.require(:item).permit(
      :name, :found_at_date, :found_at_time, :description, :internal_notes, :show_found_date, :show_found_time,
      :show_image, :image, :category_id, :expiration_date, :claim_form, :location, :flagged, :archive_reason_id,
      :tag_list, :item_storage_location_id, :is_active, item_custom_inputs_attributes: []
    )

      end

型号:

class Item < ApplicationRecord
  has_many :item_custom_inputs
  accepts_nested_attributes_for :item_custom_inputs, allow_destroy: true
end

class ItemCustomInput < ApplicationRecord
    belongs_to :item_custom_field
    belongs_to :item
end

嵌套表单字段:

<% @venue.get_custom_logging_fields_without_section.each do |field| %>
    <% field_type = field.class.name if field.present? %>
    <div class="form-group">
        <% if field_type == "ItemCustomField" %>
            <label><%= field.title if field.title.present? %></label> 
            <input type="text" name="item[item_custom_inputs_attributes][<%=field.id %>][data]", value= "<%= field.item_custom_input.data if field.item_custom_input.present? %>", id="item_item_custom_inputs_attributes_<%= field.id %>_data", class="form-control" />

        <% elsif field_type == "ItemCustomDateField" %>
            <label><%= field.title + " DATE" %></label>
            <input type="date" name="item[item_custom_date_inputs_attributes][<%=field.id %>][data]", value= "<%= field.item_custom_date_input.data if field.item_custom_date_input.present? %>", id="item_item_custom_date_inputs_attributes_<%= field.id %>_data", class="form-control" />

            <label><%= field.title + " TIME" %></label>
            <input type="time" name="item[item_custom_date_inputs_attributes][<%= field.id %>][data]", class="form-control", step: 1 />

        <% elsif field_type == "ItemCustomSelectField" %>
            <label><%= field.title if field.title.present? %></label> 
            <%= select_tag "item[item_custom_select_inputs_attributes][#{field.id}][data]", options_from_collection_for_select(field.item_custom_select_values, :id, :name), class: "form-control" %>
        <% end %>

    </div>
<% end %> 

很明显,我缺少了一些东西却找不到。我尝试使用item_params许可格式的不同组合来表示“ item_custom_inputs_attributes”,但没有任何效果。

1 个答案:

答案 0 :(得分:4)

使用嵌套属性时,应使用form.fields_for,如果模型item_custom_inputs具有属性,则应允许在控制器中使用:

def item_params
   params.require(:item).permit(
     ...,
     item_custom_inputs_attributes: [:attr1, :attr2]
  )
end

可以在以下链接中找到了解嵌套形式的好例子:https://www.pluralsight.com/guides/ruby-on-rails-nested-attributes