Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't mass-assign protected attributes: quantities
    primarykey
    data
    text
    <p>I'm getting this error:</p> <blockquote> <p>Can't mass-assign protected attributes: quantities</p> </blockquote> <p>I looked up all the threads concerning this issue in the site, but couldn't find something to answer my problem. Here are the code snippets:</p> <h3>product.rb</h3> <pre><code>class Product &lt; ActiveRecord::Base attr_accessible :name, :quantities_attributes has_many :quantities accepts_nested_attributes_for :quantities, :allow_destroy =&gt; :true, :reject_if =&gt; proc { |attrs| attrs.all? { |k, v| v.blank? } } </code></pre> <h3>new.html.erb</h3> <pre><code>&lt;% if @was_submitted %&gt; &lt;%= form_for(:new_product_array, :url =&gt; products_path) do |f| %&gt; &lt;% prefix ||= 0 %&gt; &lt;% @new_product_array.each do |n| %&gt; &lt;% n.quantities.build %&gt; &lt;% prefix += 1 %&gt; &lt;%= f.fields_for(prefix.to_s ) do |child| %&gt; &lt;div class="field"&gt; &lt;%= child.label :name %&gt;&lt;br /&gt; &lt;%= child.text_field :name%&gt; &lt;/div&gt; &lt;%= render :partial =&gt; 'quantities/form', :locals =&gt; {:form =&gt; child} %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= submit_tag :submit %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;%= form_tag new_product_path, :method =&gt; 'get' do %&gt; &lt;p align=center&gt; How many Items are you Adding? (1-100) &lt;%= number_field_tag 'amount', 1, :in =&gt; 1...100 %&gt; &lt;/br&gt; To which storage? &lt;%= number_field_tag 'storage', 1, :in =&gt; 1...100 %&gt; &lt;%= submit_tag "Next", :name =&gt; 'submitted' %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= link_to 'Back', products_path %&gt; </code></pre> <h3>product_controller.rb</h3> <pre><code>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 &lt;&lt; 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 =&gt; @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 </code></pre> <h3>quantity.rb</h3> <pre><code>class Quantity &lt; ActiveRecord::Base belongs_to :product attr_accessible :amount, :storage end </code></pre> <h3>quantity/_form.html.erb</h3> <pre><code>&lt;%= form.fields_for :quantities do |quant| %&gt; &lt;div class="field"&gt; &lt;%= quant.label :storage %&gt;&lt;br /&gt; &lt;%= quant.number_field :storage %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= quant.label :amount %&gt;&lt;br /&gt; &lt;%= quant.number_field :amount %&gt; &lt;/div&gt; &lt;% unless quant.object.nil? || quant.object.new_record? %&gt; &lt;div class="field"&gt; &lt;%= quant.label :_destroy, 'Remove:' %&gt; &lt;%= quant.check_box :_destroy %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Overall what Im trying to do, is ask the user how much products to add, then make a form with the number of fields the user specifies and with one submit button add all of the products, whereas when you add a product you also add a quantity record which holds more information on the product.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload