0

我收到此错误:

无法批量分配受保护的属性:数量

我在网站上查找了有关此问题的所有主题,但找不到可以回答我的问题的内容。以下是代码片段:

产品.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? } }

新的.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 %>

总的来说,我想做的是询问用户要添加多少产品,然后使用用户指定的字段数制作一个表单,并使用一个提交按钮添加所有产品,而当您添加产品时,您还添加了一个数量记录,其中包含有关产品的更多信息。

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 回答