Reputation: 25
I've changed my models in a number of ways, still getting this error when I create a Region. Also checked all the threads on this error here, no luck trying those changes. Here are the models:
class Region < ActiveRecord::Base
attr_accessible :name, :created_at, :country_id
belongs_to :country
has_many :winemakers, :dependent => :destroy
end
class Country < ActiveRecord::Base
attr_accessible :name, :created_at
validates :name, :presence => true, :uniqueness => {:case_sensitive => false}
has_many :regions, :dependent => :destroy
end
The error:
Can't mass-assign protected attributes: country
Application Trace | Framework Trace | Full Trace
app/controllers/regions_controller.rb:43:in `new'
app/controllers/regions_controller.rb:43:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"cBfNAgN7sE+05/X1J7QdGklFd9YrV5DO3SxeMDE1wyM=",
"region"=>{"name"=>"Test",
"country"=>"France"},
"commit"=>"Create Region"}
This is ruby 1.9.3p327, rails 3.2.9. Thank you in advance.
Upvotes: 0
Views: 284
Reputation: 1114
In your model you have country_id accessible but when you try to create you have string called country.
In your view change the country field to country_id.
Upvotes: 2
Reputation: 44675
I would probably do sth like:
class Region < ActiveRecord::Base
attr_accessible :name, :created_at, :country_id, :country
belongs_to :country
has_many :winemakers, :dependent => :destroy
def country=(value)
return super(Country.find_by_name(value)) if value.is_a? String
super
end
end
Upvotes: 0