I have a rails application that models a house. house contains rooms
and rooms have nested attributes for light and small_appliance. I have
a calculator controller, which is how end users will access the
application.
My problem is that I can't get the partial for adding rooms to submit
correctly from calculator. The initial page lets the user enter house
information, which is saved using save_house when submit is clicked.
This also redirects the user to the add_rooms page, where they can add
rooms to the house.
add_rooms displays correctly, but when I click submit, I get this
error:
Well from a cursory glance of your form, @house is nil because nowhere
do you pass an :id parameter, so you end up doing @house = House.find
(nil)
If I'm reading correctly the first time the form is rendered you're in
the add_room action so house ends up being derived from params[:room]
[:house_id] but in add_rooms @house is retrieved differently.
Fred
Hi Ryan,
The problem is you have't actually passed the house id to the room
because
<p>House id is <%= @house.id %></p>
just displays the house id to that page. But it won't be submitted
with the form
If I am getting right you have a relationship in house model like
has_many :rooms
ok
Let me explain that to get the house id in controller the house.id
must be submitted to the controller
You can do this by two ways
1. Submitting house.id via hidden field
use <%= hidden_field :house_id%>
Note to get this running it must be set when building new room object
build using
@house.rooms.build() OR
Room.new(:house_id => @house.id)
If this one didn't work contact me at
bagwanpankaj@gmail.com or
blog.railsjaipur.in
I'll tell you a second solution.
Thanks pankaj88, that worked nicely.