Koli
Koli

Reputation: 317

Can't mass-assign protected attributes. (attr_accessible is set)

I make a carpooling application. When I want to create a new Route, I have this error:

ActiveModel::MassAssignmentSecurity::Error in RoutesController#create
Can't mass-assign protected attributes: place_ids

The Models are looking like this:

class Route < ActiveRecord::Base
  #  id             :integer         not null, primary key
  #  start_place_id :integer
  #  end_place_id   :integer
  #  start_time     :datetime
  #  end_time       :datetime
  #  car_id         :integer
  belongs_to :car
  has_many :places
  has_and_belongs_to_many :users
  attr_accessible :start_place_id, :end_place_id, :start_time, :end_time, :car_id
end

class Place < ActiveRecord::Base
  has_many :routes
  attr_accessible :name, :address, :lat, :long, :description
end

class Car < ActiveRecord::Base
  has_many :routes
  attr_accessible :car_type, :license_plate, :seats_num, :motorway_vignette_expeier
end

The _form.html.erb of the Routes looking like this:

<%= simple_form_for @route do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <%= f.association :places %>
  <%= f.association :places %>
  <%= f.input :start_time %>
  <%= f.input :end_time %>
  <%= f.association :car %>
  <%= f.button :submit %>
<% end %>

The log is writing this:

Started POST "/routes" for 127.0.0.1 at 2012-03-23 15:41:37 +0100
Processing by RoutesController#create as HTML
  Parameters: {"utf8"=>"✓", "route"=>{"place_ids"=>["", "4", "", "1"], "start_time(1i)"=>"2012", "start_time(2i)"=>"3", "start_time(3i)"=>"23", "start_time(4i)"=>"14", "start_time(5i)"=>"41", "end_time(1i)"=>"2012", "end_time(2i)"=>"3", "end_time(3i)"=>"23", "end_time(4i)"=>"14", "end_time(5i)"=>"41", "car_id"=>"1"}, "commit"=>"Create Route"}
Completed 500 Internal Server Error in 1ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: place_ids):
  app/controllers/routes_controller.rb:15:in `new'
  app/controllers/routes_controller.rb:15:in `create'

I don't understand, why it makes a 4 element long array for place_ids ("place_ids"=>["", "4", "", "1"], ). And I don't understand, why it write MassAssignment, when I wrote in the model attr_accessible... I did similar in rails 3.0.0, and it worked. I think simple_form changed something... Why he write place_ids? Why not start_place_id and end_place_id?

I uploaded the full project to github: https://github.com/Koli14/telekocsi2

The environment is:

ruby 1.9.2p290

Rails 3.2.1

simple_form (2.0.1)

Ubuntu 11.10

Upvotes: 0

Views: 5386

Answers (3)

clemensp
clemensp

Reputation: 2510

You probably want explicit start and end places:

class Route < ActiveRecord::Base
  belongs_to :car
  has_one :start_place, :class => "Place", :foreign_key => "start_place_id"
  has_one :end_place, :class => "Place", :foreign_key => "end_place_id"
  has_and_belongs_to_many :users
  attr_accessible :start_place_id, :end_place_id, :start_time, :end_time, :car_id
end

along with:

<%= simple_form_for @route do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <%= f.association :start_place %>
  <%= f.association :end_place %>
  <%= f.input :start_time %>
  <%= f.input :end_time %>
  <%= f.association :car %>
  <%= f.button :submit %>
<% end %>

Upvotes: 1

Ricardo Acras
Ricardo Acras

Reputation: 36244

When you used attr_accessible in your model, you told Rails to only accept those attributes via mass assignment.

In order to assign any other attribute you must specifically set it in your code.

But, as I can see by your code, you want to assign values for the places has_many association. In that case maybe you could try and use accepts_nested_attributes_for, so the model will accept the detail info.

Upvotes: 1

John Plummer
John Plummer

Reputation: 1044

I think you probably want another look at your associations. It seems to me that a route should belong to an end place and belong to a start place rather than have many places (unless you are including waypoints). A clue that this may be the case is the foreign keys in your route model.

Upvotes: 0

Related Questions