joelm
joelm

Reputation: 8771

rails: Can't mass-assign protected attributes

This seems to be a common error, and I've read many replies. I must be missing something dumb.

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:

Can't mass-assign protected attributes: address, location_name, phone_number, district, postcode, city, country, lat,

class Activity < ActiveRecord::Base
  attr_accessible :beer, :user, :whatdoing, :where, :with, :location_id, :location_attributes

  has_one :location, :dependent => :destroy

  accepts_nested_attributes_for :location


  validates :whatdoing, :numericality => { :only_integer => true, :greater_than => -1}
end


class Location < ActiveRecord::Base
  validates_presence_of :address

  belongs_to :activity

end

Activity Controller:

# 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

It must be something simple, but I can't spot it.

Thanks in advance!

Adding related form code:

<%= form_for(@activity) do |activity_form| %>
  <% if @activity.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@activity.errors.count, "error") %> prohibited this activity from being saved:</h2>

      <ul>
      <% @activity.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= activity_form.label :whatdoing, "Thinkin" %>
    <%= activity_form.radio_button :whatdoing, 0 %>
    <%= activity_form.label :whatdoing, "Drinkin" %>
    <%= activity_form.radio_button :whatdoing, 1 %>
  </div>
  <div class="field">
    <%= activity_form.label :beer %>
    <%= activity_form.text_field :beer %>
  </div>
  <div class="field">
    <%= activity_form.label :where %>
    <%= activity_form.text_field :where %>
  </div>
  <div class="field">
    <%= activity_form.label :with %>
    <%= activity_form.text_field :with %>
  </div>

<%= activity_form.fields_for :location do |location_fields| %>
<div class="field search">
    <%= label_tag :search %>
    <%= text_field_tag :location_search, nil, :size => 60, :type => "search" %>
  </div>

  <br />

  <div class="field">
    <%= location_fields.label :address, "Full Address" %>
    <%= location_fields.text_field :address, :size => 60 %>
  </div>

  <div class="field">
    <%= location_fields.label :location_name %>
    <%= location_fields.text_field :location_name, :size => 18 %>

    <%= location_fields.label :phone_number %> 
    <%= location_fields.text_field :phone_number, :size => 18 %>
  </div>

  <div class="field">
    <%= location_fields.label :district %>
    <%= location_fields.text_field :district, :size => 18 %>
    <%= location_fields.label :postcode %>
    <%= location_fields.text_field :postcode, :size => 6, :type => 'number' %>
  </div>

  <div class="field">
    <%= location_fields.label :city %>
    <%= location_fields.text_field :city, :size => 18 %>

    <%= location_fields.label :country %> 
    <%= location_fields.text_field :country, :size => 18 %>
  </div>

  <div class="field">
    <%= location_fields.label :lat %>
    <%= location_fields.text_field :lat, :size=> 18 %>
    <%= location_fields.label :lng %>
    <%= location_fields.text_field :lng, :size=> 18 %>
  </div>
<% end %>

  <%= render :partial => "googlemap" %>


  <div class="actions">
    <%= activity_form.submit %>
  </div>

<% end %>

Upvotes: 0

Views: 842

Answers (3)

joelm
joelm

Reputation: 8771

turns out I was missing the attr_accessible on the location model for all the attributes. Added them and all is well.

Upvotes: 0

YaBoyQuy
YaBoyQuy

Reputation: 793

In Rails 3 and up, you need attr_writer or attr_accessor, attr_accessible for mass assigning attributes

you have to create some write privileges for these attributes

address, location_name, phone_number, district, postcode, city, country, lat,

Upvotes: 0

Harry
Harry

Reputation: 4835

Without seeing your form with the nested fields, from the error it looks like you may have omitted the fields_for loop for location - it should look something like

<%= f.fields_for :location do |f| %>
    <%= render :partial => "locations/form", :locals => {:f => f} %>
<% end -%>

This should load the form for the nested fields, and ensure that you are not trying to set attributes for the location on the activity.

Upvotes: 1

Related Questions