I have two models Hotel and Address. Relationships are:
class Hotel
belongs_to :user
has_one :address
accepts_nested_attributes_for :address
You are using wrong method
for appending your child with the parent.And also it is has_one relation
,so you should use build_model
not model.build
.Your new
and create
methods should be like this
class HotelsController < ApplicationController
def new
@hotel = Hotel.new
@hotel.build_address #here
end
def create
@hotel = current_user.hotels.build(hotel_params)
if @hotel.save
flash[:success] = "Hotel created!"
redirect_to @hotel
else
render 'new'
end
end
Update
Your hotel_params
method should look like this
def hotel_params
params.require(:hotel).permit(:title, :stars, :room, :price,address_attributes: [:country,:state,:city,:street])
end