Reputation: 161
I have an Address model which belongs to ParkingCompany or User. each address can belong to more than one user. but each ParkingCompany has one Address.
I get Can't mass-assign protected attributes: addresses
error on my form. here are my codes
ParkingCompany model:
class ParkingCompany < ActiveRecord::Base
attr_accessible :company_id, :description, :email, :telephone, :website, :company_name, :addresses_attributes
has_many :parking_branch
has_one :address
accepts_nested_attributes_for :address
end
Address model:
class Address < ActiveRecord::Base
attr_accessible :address1, :address2, :address3, :address_id, :city, :country, :county, :house_name, :postcode, :parking_companies_attributes
belongs_to :parking_companies
has_many :users
end
when I try to add a new company via below form, gives me 'Can't mass-assign protected attributes: addresses' error
my nested form, new.html.erb:
<h1>New Company</h1>
<%= simple_form_for @parking_company do |f| %>
<%= f.error_notification %>
<%= f.input :company_name, :required => true %>
<%= f.input :email, :required => true %>
<%= f.input :website %>
<%= f.input :description, :as => :text, :input_html => { :rows => 3 } %>
<%= f.input :telephone, :required => false %>
<%= f.simple_fields_for :addresses do |a| %>
<%= a.input :address1 %>
<%= a.input :address2, :required => false %>
<%= a.input :address3, :required => false %>
<%= a.input :city %>
<%= a.input :county %>
<%= a.input :postcode %>
<%= a.country_select :country, ["United Kingdom"], { keys: :alpha3s, values: :names } %>
<% end %><br/>
<%= f.button :submit, 'Create new company', :class => 'btn-primary' %>
<% end %>
just in case, this is parking_companies_controller
class ParkingCompaniesController < ApplicationController
def index
authorize! :index, @parking_company, :message => 'Not authorized as an administrator.'
@parking_companies = ParkingCompany.all
end
def show
@parking_company = ParkingCompany.find(params[:id])
end
def new
@parking_company = ParkingCompany.new
end
def create
@parking_company = ParkingCompany.create(params[:parking_company])
end
def update
end
def destroy
end
end
Upvotes: 0
Views: 142
Reputation: 3205
There is a has_one :address
and then you refer to :addresses
in the form.
Upvotes: 2