Newbie here, running rails 3.2.2 and ruby 1.8.7. I have 2 models, Hotel and Facility, they both have have_one abd belongs_to with foriegn jey created correctly.
The goal is, in a single form have displayed info. of both Hotel and Facility. I've seen nested forms but i always get the same error. I am able to display the form correctly, but when i try to save, this shows up: ActiveModel::MassAssignmentSecurity::Error in HotelsController#create Can't mass-assign protected attributes: @hotel
app/controllers/hotels_controller.rb:60:in `new' app/controllers/hotels_controller.rb:60:in `create'
However, 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 :room24h, :concierge, :hotel_id #attr_accessible :concierge, :hotel_id, :room24h, :hotel end
My facility controller is empty, and my hotel_controller new /create is:
def new @hotel = Hotel.new @facility = @hotel.build_facility
respond_to do |format| format.html # new.html.erb format.json { render :json => @hotel } end end
def create
@hotel = Hotel.new(params[:hotel]).facility.new(params[:facility])
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
Then when i click create, the error I wrote earlier happens. Why? I believe have all the necessary attr_accessible fields
Any tips? Thanks, I'm deeply stuck