If I'm not wrong, this is what you need: http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf
On Oct 6, 10:37 am, ryan8720 <[email protected]> wrote: > This seems to be working nicely. However, the room_id is not being > saved into the Lights table. What am I missing. > > Also, how to I get each set of text boxes to save to a new row in the > Lights table? > > - Ryan > > On Oct 6, 2:28 am, Salil Gaikwad <[email protected]> > wrote: > > > > > Hi Ryan, > > > 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? && �[email protected] > > && @light.save && @house.add_room(@room) && @house.save > > flash[:notice] = "Room \"#[email protected]}\" 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 viahttp://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

