Try this out
<% form_for :room, :url => { :action => :add_room, :id => @house } do
|form| %>
<%= form.label :name, "Name of room: " %>
<%= form.text_field :name %>
/* Differentiate lights parameter using following way */
<%= text_field :light,:number_of_lights, :size => 2 %><br />
<%= text_field :light, :wattage, :size => 2 %><br />
<% end %>
And in your controller do the following
def add_room
@house = House.find(params[:id])
@room = Room.new(params[:room])
@light = Light.new(params[:light])
respond_to do |format|
## Save all the parameter only if they are valid, if any of your
object is not pass validation then no one of the below get saved.
if @room.valid? && @light.valid? && @house.valid? && @room.save
&& @light.save && @house.add_room(@room) && @house.save
flash[:notice] = "Room \"#{@room.name}\" was successfully
added."
format.html { render :action => 'add_rooms' }
format.xml { render :xml => @room, :status
=> :created, :location => @room }
else
format.html { render :action => 'add_rooms' }
format.xml { render :xml => @room.errors, :status
=> :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid house #{params[:id]}")
flash[:notice] = "You must create a house before adding a room"
redirect_to :action => 'index'
end
try it & let me know if u face any problem
Thanks,
Salil Gaikwad
--
Posted via http://www.ruby-forum.com/.
http://github.com/ryanb/complex-form-examples
You can git the repository and run the app to see how it works and then
dig into the code.
Note: the javascript library used in the example app is prototype, but
it is pretty easy to replace it with another library (JQuery, for
example).
<% javascript_include_tag :defaults %>
Cool, good work!