控制台中未允许的参数

时间:2017-11-12 07:55:13

标签: ruby-on-rails

模特

prd_item.rb

has_many:prd_allisland_flat_deliveries,dependent :: destroy,inverse_of :: prd_item

prd_allisland_flat_delivery.rb

belongs_to:prd_item

主页中的

 <%=  p.fields_for :prd_allisland_flat_deliveries  do |i| %>

   <%= render(:partial => 'prd_allisland_flat_delivery_field', :locals => {:f => i})%>
 <% end %>

在prd_allisland_flat_delivery_field中形成部分

   <div class="row" style="padding-bottom: 25px">
          <div class="col-md-2"></div>
          <div class="col-md-4">
            <%= f.label :delivery_period %>
          </div>



            <div class="col-md-4">


     <%= f.text_field(:delivery_period, {placeholder: '0', class: 'form-control input_border input_field_text_align_right'})%>
          </div>
          <div class="col-md-2"></div>
   </div>

        <div class="row" style="padding-bottom: 25px">
          <div class="col-md-2"></div>
          <div class="col-md-4">
            <%= f.label :delivery_rate %>
          </div>
          <div class="col-md-4">
            <%= f.text_field(:delivery_rate, {placeholder: 'Rs. 0.00', class: 'form-control input_border input_field_text_align_right'})%>
          </div>

        </div> 

在prd_item控制器中

** def new

@item = PrdItem.new

@item.prd_allisland_flat_deliveries.build

端**

写完后创建方法

**

if @item.save

  if @item.delivery_type == 1

     @all_island_flat = @item.prd_allisland_flat_deliveries.build(item_params[:prd_allisland_flat_deliveries_attributes])
    @all_island_flat.save
  end

端**

item_params

def item_params

    params.require(:prd_item).permit(:item_name, :brand, :item_no, :short_description, :long_description, :prd_type_id, :usr_vendor_property_id, :price,:base_price, :price_discount, :percentage_discount, :stock_count, :availability, :tags, :remove_image, :delivery_type , :min_stock_count,

                                 prd_item_images_attributes: [:id, :image, :description, :link, :_destroy ],

                                 prd_temp_variation_stores_attributes: [:id, :product_variations, :variation_items, :_destroy],
                                 prd_temp_compound_stores_attributes:[:id,:compound, :compound_item, :_destroy],
                                 prd_temp_spec_stores_attributes:[:id,:compound, :compound_item, :_destroy],
                                 prd_allisland_flat_deliveries_attributes: [:id,:delivery_period,:delivery_rate],
                                 prd_province_vise_deliveries_attributes: [:id , :province_name , :delivery_rate, :delivery_period]

                                 )

rails consoler获取未经许可的参数:prd_allisland_flat_deliveries

任何人都可以解释原因

view

enter image description here

enter image description here

如果图片不清晰

参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; authenticity_token&#34; =&gt;&#34; ZqJOX4nMmWyVG2CxJznKKxLLsUkG + 4ndBFdvxfx2TPouiQkIbYfvQ00moCdqFZWPd0nJ4ipCVY9JhVToESDCoQ ==&#34 ;,&#34; prd_item&#34; =&gt; {&#34; item_name&#34; =&gt;&#34; abc&#34;,&#34; item_no&#34; =&gt;&#34; 666333& #34;,&#34;品牌&#34; =&gt;&#34;&#34; ,&#34; prd_type_id&#34; =&gt;&#34;&#34;,&#34; short_description&#34; =&gt;&#34;&#34;,&#34; long_description&#34; = &gt;&#34;&#34;,&#34;标签&#34; =&gt;&#34;&#34;,&#34; prd_item_images_attributes&#34; =&gt; {&#34; 0&#34 ; =&gt; {&#34; _destroy&#34; =&gt;&#34; false&#34;,&#34;说明&#34; =&gt;&#34;&#34;,&#34;链接& #34; =&gt;&#34;&#34;}},&#34; base_price&#34; =&gt;&#34; 655&#34;,&#34;价格&#34; =&gt;&# 34; 688 &#34;,&#34; price_discount&#34; =&gt;&#34; 5&#34;,&#34; prd_temp_spec_stores_attributes&#34; =&gt; {&#34; 0&#34; =&gt; {& #34; spec_item&#34; =&gt;&#34;&#34;,&#34; spec&#34; =&gt;&#34;&#34;}},&#34; prd_temp_variation_stores_attributes&#34; = &gt; {&#34; 0&#34; =&gt; {&#34; product_variations&#34; =&gt;&#34;&#34;,&#34; variation_items&#34; =&gt;&#34; &#34;}},&#34; prd_temp_ compound_stores_attributes&#34; =&gt; {&#34; 0&#34; =&gt; {&#34; compound_item&#34; =&gt;&#34;&#34;,&#34; compound&#34; =&gt; ;&#34;&#34;}},&#34; stock_count&#34; =&gt;&#34; 55&#34;,&#34; min_stock_count&#34; =&gt;&#34; 5&#34 ;,&#34;可用性&#34; =&gt;&#34;可用&#34;,&#34; delivery_type&#34; =&gt;&#34; 1&#34;,&#34; prd_allisland_flat_deliveries&#34; =&GT; {&#34; d elivery_period&#34; =&gt;&#34; 255&#34;,&#34; delivery_rate&#34; =&gt;&#34; 22&#34;},&#34; prd_province_vise_deliveries&#34; =&gt; {& #34; delivery_rate&#34; =&gt;&#34;&#34;,&#34; delivery_period&#34; =&gt;&#34;&#34;}},&#34; commit&#34; = &gt;&#34;添加产品&#34;}

未经许可的参数:prd_allisland_flat_deliveries,prd_province_vise_deliveries

SQL(1.0ms)INSERT INTO&#34; prd_allisland_flat_deliveries&#34; (&#34; prd_item_id&#34;,&#34; created_at&#34;,&#34; updated_at&#34;)价值($ 1,$ 2,$ 3)返回&#34; id&#34; [[&#34; prd_item_id&#34;,52],[&#34; created_at&#34;,2017-11-12 14:02:09 UTC],[&#34; upd ated_at&#34;,2017-11-12 14:02:09 UTC]]

1 个答案:

答案 0 :(得分:0)

你的问题是:

permit(
  ..., 
  :prd_allisland_flat_deliveries_attributes,
  :prd_province_vise_deliveries_attributes
)

但是,您不允许:prd_allisland_flat_deliveries:prd_province_vise_deliveries。这就是你在params中所拥有的。

因此,您会收到Unpermitted参数错误。允许这些参数,或修改您的部分参数以包含_attributes

允许他们:

params.require(:prd_item).permit(
  :item_name, 
  :brand, 
  :item_no, 
  :short_description, 
  :long_description, 
  :prd_type_id, 
  :usr_vendor_property_id, 
  :price,:base_price, 
  :price_discount, 
  :percentage_discount, 
  :stock_count, 
  :availability, 
  :tags, 
  :remove_image, 
  :delivery_type , 
  :min_stock_count,
  prd_item_images_attributes: [:id, :image, :description, :link, :_destroy ],
  prd_temp_variation_stores_attributes: [:id, :product_variations, :variation_items, :_destroy],
  prd_temp_compound_stores_attributes:[:id,:compound, :compound_item, :_destroy],
  prd_temp_spec_stores_attributes:[:id,:compound, :compound_item, :_destroy],
  prd_allisland_flat_deliveries: [:id,:delivery_period,:delivery_rate],
  prd_province_vise_deliveries: [:id , :province_name , :delivery_rate, :delivery_period]
)