Note that there are some explanatory texts on larger screens.

plurals
  1. POrails: Can't mass-assign protected attributes
    primarykey
    data
    text
    <p>This seems to be a common error, and I've read many replies. I must be missing something dumb.</p> <p>I have an activity with one location. (Has_one). I've set accepts_nested_attributes_for :location, I believe I have the singular/plural right (all singular, it's a has_one relationship). (I have tried listing the attr_accessible for each attribute individually, no dice.) I have restarted my server. yet, I keep getting:</p> <blockquote> <p>Can't mass-assign protected attributes: address, location_name, phone_number, district, postcode, city, country, lat,</p> </blockquote> <pre><code>class Activity &lt; ActiveRecord::Base attr_accessible :beer, :user, :whatdoing, :where, :with, :location_id, :location_attributes has_one :location, :dependent =&gt; :destroy accepts_nested_attributes_for :location validates :whatdoing, :numericality =&gt; { :only_integer =&gt; true, :greater_than =&gt; -1} end class Location &lt; ActiveRecord::Base validates_presence_of :address belongs_to :activity end </code></pre> <p><strong>Activity Controller:</strong></p> <pre><code># GET /activities/new.json def new @activity = Activity.new @activity.build_location respond_to do |format| format.html # new.html.erb format.json { render json: @activity } end end # PUT /activities/1 # PUT /activities/1.json def update @activity = Activity.find(params[:id]) respond_to do |format| if @activity.update_attributes(params[:activity]) format.html { redirect_to @activity, notice: 'Activity was successfully updated.' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @activity.errors, status: :unprocessable_entity } end end end </code></pre> <p>It must be something simple, but I can't spot it. </p> <p>Thanks in advance!</p> <p>Adding related form code:</p> <pre><code>&lt;%= form_for(@activity) do |activity_form| %&gt; &lt;% if @activity.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@activity.errors.count, "error") %&gt; prohibited this activity from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @activity.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= activity_form.label :whatdoing, "Thinkin" %&gt; &lt;%= activity_form.radio_button :whatdoing, 0 %&gt; &lt;%= activity_form.label :whatdoing, "Drinkin" %&gt; &lt;%= activity_form.radio_button :whatdoing, 1 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= activity_form.label :beer %&gt; &lt;%= activity_form.text_field :beer %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= activity_form.label :where %&gt; &lt;%= activity_form.text_field :where %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= activity_form.label :with %&gt; &lt;%= activity_form.text_field :with %&gt; &lt;/div&gt; &lt;%= activity_form.fields_for :location do |location_fields| %&gt; &lt;div class="field search"&gt; &lt;%= label_tag :search %&gt; &lt;%= text_field_tag :location_search, nil, :size =&gt; 60, :type =&gt; "search" %&gt; &lt;/div&gt; &lt;br /&gt; &lt;div class="field"&gt; &lt;%= location_fields.label :address, "Full Address" %&gt; &lt;%= location_fields.text_field :address, :size =&gt; 60 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= location_fields.label :location_name %&gt; &lt;%= location_fields.text_field :location_name, :size =&gt; 18 %&gt; &lt;%= location_fields.label :phone_number %&gt; &lt;%= location_fields.text_field :phone_number, :size =&gt; 18 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= location_fields.label :district %&gt; &lt;%= location_fields.text_field :district, :size =&gt; 18 %&gt; &lt;%= location_fields.label :postcode %&gt; &lt;%= location_fields.text_field :postcode, :size =&gt; 6, :type =&gt; 'number' %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= location_fields.label :city %&gt; &lt;%= location_fields.text_field :city, :size =&gt; 18 %&gt; &lt;%= location_fields.label :country %&gt; &lt;%= location_fields.text_field :country, :size =&gt; 18 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= location_fields.label :lat %&gt; &lt;%= location_fields.text_field :lat, :size=&gt; 18 %&gt; &lt;%= location_fields.label :lng %&gt; &lt;%= location_fields.text_field :lng, :size=&gt; 18 %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;%= render :partial =&gt; "googlemap" %&gt; &lt;div class="actions"&gt; &lt;%= activity_form.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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