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:
<% # form_for :room, :url => { :action => :add_room, :id => @house }
do |form| %>
<legend>Add a Room</legend>
<div>
<%= form.label :name, "Name of room: " %>
<%= form.text_field :name %>
</div>
<p><strong>Lights</strong></p>
<p class="description tier2">Enter lighting information for up to
three different types of lights.
<div id="lights0">
<%= form.label :number_of_lights, "Number of lights: " %>
<%= form.text_field :number_of_lights, :size => 2 %><br />
<%= form.label :wattage, "Wattage of lights: " %>
<%= form.text_field :wattage, :size => 2 %><br />
<%= form.label :hours_used_per_day, "Number of hours per day
lights are on: " %>
<%= form.text_field :hours_used_per_day, :size => 2 %>
<div id="lights1">
<%= form.label :number_of_lights1, "Number of lights: " %>
<%= form.text_field :number_of_lights1, :size => 2 %><br />
<%= form.label :wattage1, "Wattage of lights: " %>
<%= form.text_field :wattage1, :size => 2 %><br />
<%= form.label :hours_used_per_day1, "Number of hours per day
lights are on: " %>
<%= form.text_field :hours_used_per_day1, :size => 2 %>
<div id="lights2">
<%= form.label :number_of_lights2, "Number of lights: " %>
<%= form.text_field :number_of_lights2, :size => 2 %><br />
<%= form.label :wattage2, "Wattage of lights: " %>
<%= form.text_field :wattage2, :size => 2 %><br />
<%= form.label :hours_used_per_day2, "Number of hours per day
lights are on: " %>
<%= form.text_field :hours_used_per_day2, :size => 2 %>
<%= submit_tag "Add room" %>
<% end %>
And here is the code snippet from the controller.
def add_room
@house = House.find(params[:id])
@room = Room.new(params[:room])
@light = Light.new(params[:light])
respond_to do |format|
if @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
My problem is that I can’t get this form to save the room and lights
(and later small_appliances). After reading various sources I was
trying to use <% form_tag({:controller => “calculator”, :action =>
“add_room”} ) %> instead of form_for, but I’m not sure how to access
the params correctly. How can I save to both tables?
Thanks in advance,
Ryan