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

时间:2012-09-02 21:26:43

标签: ruby-on-rails

我收到了这个错误:

  

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

我在网站上查找了有关此问题的所有主题,但无法找到解决问题的方法。以下是代码段:

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

quantity.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 %>

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

2 个答案:

答案 0 :(得分:1)

你需要这样的一行:

attr_accessible :name, :quantities_attributes, :quantities

答案 1 :(得分:0)

你的代码非常糟糕,100%可以简单得多。

您的问题是表单不会对您的资源(产品)一无所知,因此它无法呈现“智能”字段“quantity_attributes”,而是呈现“数量”。