0

このエラーが発生します:

保護された属性を一括割り当てできません:数量

この問題に関するすべてのスレッドをサイトで調べましたが、問題に答える何かが見つかりませんでした。コードスニペットは次のとおりです。

product.rb

class Product < ActiveRecord::Base
  attr_accessible :name, :quantities_attributes
  has_many :quantities

  accepts_nested_attributes_for :quantities, :allow_destroy => :true,
  :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }

new.html.erb

<% if @was_submitted %>
  <%= form_for(:new_product_array, :url => products_path) do |f| %>
    <% prefix ||= 0 %>
    <% @new_product_array.each do |n| %>
      <% n.quantities.build %>
      <% prefix += 1 %>
      <%= f.fields_for(prefix.to_s ) do |child| %>
        <div class="field">
          <%= child.label :name %><br />
          <%= child.text_field :name%>
        </div>
        <%= render :partial => 'quantities/form',
                   :locals => {:form => child} %>
      <% end %>
    <% end %>
    <div class="actions">
      <%= submit_tag :submit %>
    </div>
  <% end %>
<% else %>
  <%= form_tag new_product_path, :method => 'get' do %>
  <p align=center>
    How many Items are you Adding? (1-100)
    <%= number_field_tag 'amount', 1, :in => 1...100 %>
    </br>
    To which storage?
    <%= number_field_tag 'storage', 1, :in => 1...100 %>
    <%= submit_tag "Next", :name => 'submitted' %>
  </p>
  <% end %>
<% end %>
<%= link_to 'Back', products_path %>

product_controller.rb

def new
  @product = Product.new

  if params['submitted'] 
    @was_submitted = true
    @amount_form = params['amount']
    @new_product_array = []
    (1..@amount_form.to_i).each do
      @new_product_array << Product.new
    end
    @storage_form = params['storage']
  else
    @was_submitted = false
  end

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @product }
  end
end

def create
  i=0
  logger.info params[:new_product_array].inspect
  params[:new_product_array].each do |new_product|
    if new_product.last[:name] != nil
      @new_product_array[i] = Product.new(new_product.last)
      @new_product_array[i].save
      i+=1
    end
  end
  redirect_to(products_path)
end

量.rb

class Quantity < ActiveRecord::Base
  belongs_to :product
  attr_accessible :amount, :storage
end

数量/_form.html.erb

<%= form.fields_for :quantities do |quant| %>
  <div class="field">
    <%= quant.label :storage %><br />
    <%= quant.number_field :storage %>
  </div>
  <div class="field">
    <%= quant.label :amount %><br />
    <%= quant.number_field :amount %>
  </div>
  <% unless quant.object.nil? || quant.object.new_record? %>
    <div class="field">
      <%= quant.label :_destroy, 'Remove:' %>
      <%= quant.check_box :_destroy %>
    </div>
  <% end %>
<% end %>

全体として、Imがやろうとしていることは、ユーザーに追加する製品の量を尋ね、ユーザーが指定したフィールド数でフォームを作成し、1つの送信ボタンですべての製品を追加するのに対し、製品を追加すると、製品に関する詳細情報を保持する数量レコード。

4

2 に答える 2

1

次のような行が必要です。

attr_accessible :name, :quantities_attributes, :quantities
于 2012-09-02T21:32:57.410 に答える
0

あなたは非常に悪いコードを持っています、それは100%はるかに単純になる可能性があります。

問題は、フォームがリソース(製品)について何も知らないため、「スマートに」フィールド「quantities_attributes」をレンダリングできず、代わりに「quantities」をレンダリングすることです。

于 2012-09-02T21:51:27.727 に答える