Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top