Updating multiple tables with one form

Hi all,

Here is the setup:

I'm writing an application to model a house. Houses have rooms. I need the room to have a name, an id, and then one or more lights. Each light has wattage, hours_per_day, and quantity. I'd also like the room to have many small_appliances, but I haven't gotten that far yet.

The best I've been able to figure out is to have a separate table for Lights so that a room can have many lights. My setup is a Houses table that has_many Rooms. I have a Rooms table that has_many Lights. I have a page where you can add a Room to a House.

On this page there is a text_field for name, and 3 sets of text_fields for the lights, since I can't figure out a way to allow the user to dynamically add extra lights. The code snippet:

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? && @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

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

If I'm not wrong, this is what you need:

This example is perfect. However, for some reason, it isn't working. When I click the Add Light link it doesn't add any fields. Here is all the relevant code (I think). Basically, for me, project is room, and task is light.

Yes, I have them included in the rooms.html.erb and it shows in the source of the page. I don't have an application.html.erb. I also tried moving the code from application_helper to rooms_helper, which didn't help.

I don't really understand where application comes from anyway. I didn't generate anything called application.

I figured it out. I hadn't put the functions into application.js.

ryan8720 wrote:

I figured it out. I hadn't put the functions into application.js.

Cool, good work!

Thanks for all the help so far. I believe I'm starting to understand things now. The part above is working great. However, I have a controller called calculator which is the only part that will be accessible to users. From calculator you enter information about the house, then that information is saved to house and you can then start adding rooms, have a house_id attribute that needs to be passed from the house that was just created.

The problem now is that I can't get the new room stuff to render from calculator. It always complains it can't find the partials (which are located under app/views/rroms). If I change the paths for them to be found, it causes other errors.

Here is the calculator_controller. save_house is called when the user submits the form. The commented line under "if @house.save" is what I had originally. The line under that is my attempt to fix these problems.