I posted a questionnot long ago, yet I still cannot get pass this simple
issue.
Begginer Running rails 3.2.2, Ruby 1.8.7
I have 2 models, a Hotel (created by scaffolding) and Facility (with
empty controller). I am able to set the 1-to-1 association and siplaying
fields but can’t seem to insert it in the databases. Im getting:
ActiveModel::MassAssignmentSecurity::Error in HotelsController#create
Can’t mass-assign protected attributes: @hotel
app/controllers/hotels_controller.rb:48:in ‘new’
app/controllers/hotels_controller.rb:48:in ‘create’
–> My models are:
class Hotel < ActiveRecord::Base
has_one :facility, :dependent => :destroy
accepts_nested_attributes_for :facility, :allow_destroy => true
attr_accessible :name, :rating, :recommended, :facility_attributes
end
class Facility < ActiveRecord::Base
belongs_to :hotel
attr_accessible :concierge, :hotel_id, :room24h
end
My facility controller, as I said, is empty (it can be, right? since im
associating with Hotel?) My hotel_controller is the default after
creating scaffolding, only with 1 added line:
def new
@hotel = Hotel.new
@hotel.build_facility #–>I added this only, I searched and all i
found was this
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @hotel }
end
end
def create
@hotel = Hotel.new(params[:hotel])
respond_to do |format|
if @hotel.save
format.html { redirect_to @hotel, :notice => 'Hotel was
successfully created.’ }
format.json { render :json => @hotel, :status => :created,
:location => @hotel }
else
format.html { render :action => “new” }
format.json { render :json => @hotel.errors, :status =>
:unprocessable_entity }
end
end
end
Finaly, my form html is:
<%= form_for(@hotel) do |f| %>
<%= f.text_field :name %>
<%= f.number_field :rating %>
<%= f.check_box :recommended %>
<h2>Hotel Facilities</h2>
<%= f.fields_for :@hotel do |facility_fields| %>
Thanks in advance and sorry, im new at RoR