How do I build this form?

Hi,

I have a simple application. There is a form of items, in which a
user can say I want the item or I do not. Then there is an orders
table that records what items the user selected. The names of my
tables are "forms", "form_items" (which links back to forms),
"orders", and "line_items" (which links back to orders). I'm getting
a compile error, "line #42: undefined local variable or method
`ec_line_item' for #<ActionView::Base:0xb78edb10>" when I try and
compile the below ...

<% form_for :ec_order, :url => 'summary' do |f| %>
<% for form_item in @form.form_items %>
<%= error_messages_for :ec_line_item %>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
     <tr><td style="background-color:#FFFFCC;">
       <table cellpadding="3" cellspacing="0" border="0" width="100%">
         <tr>
           <td align="center">
                <p><label for="description">Description</label><br/>
                <%= form_item.description %></p>
           </td>
           <td align="center">
                <%= ec_line_item.check_box(:quantity, '', {}, "Request
Item", '') %>
           </td>
         </tr>
       </table>
     </td></tr>
</table>
<% end %>
<% end %>

Program is complaining about the line, "<%=
ec_line_item.check_box(:quantity, '', {}, "Request Item", '') %>".
Yeah, I didn't expect it to work, but I had to try something. Anyway,
your help is appreciated, - Dave

Hi,

I have a simple application. There is a form of items, in which a
user can say I want the item or I do not. Then there is an orders
table that records what items the user selected. The names of my
tables are "forms", "form_items" (which links back to forms),
"orders", and "line_items" (which links back to orders). I'm getting
a compile error, "line #42: undefined local variable or method
`ec_line_item' for #<ActionView::Base:0xb78edb10>" when I try and
compile the below ...

That makes sense - you haven't defined ec_line_item anywhere. What is
it supposed to be ?

Fred

What I would like to say is if a user wants a form_item, they can
check a box and an order will be created with a line_item object
corresponding to each form_item object the user checked. Can't figure
out how to make a checkbox that ties the form item to the line item.

- Dave

What I would like to say is if a user wants a form_item, they can
check a box and an order will be created with a line_item object
corresponding to each form_item object the user checked. Can't figure
out how to make a checkbox that ties the form item to the line item.

Not entirely sure if it's what you meant but if you have
<% for form_item in @form.form_items %>
   <%= check_box_tag('ec_order[form_items][]', form_item.id) %>
<%end%>

then params[:ec_order][:form_items] will be an array containing the
ids of the checked form_items.

Fred

Without knowing any of my code, you hit the nail right on the head.
Thanks for this. I have a quick follow up. In my controller, I'm
trying to create order line items, but I'm getting stuck at a very
basic place. How do I add my line item object to my order object?
Here's what I have so far ...

                @ec_order = EcOrder.new(params[:ec_order])
                for form_item_id in params[:ec_order][:form_items]
                        @form_item = FormItem.find(form_item_id)
                        EcLineItem.new(:form_item_id => form_item_id)
                        # How do I add line item to order?
                end

Thanks again for the help, - Dave

Without knowing any of my code, you hit the nail right on the head.
Thanks for this. I have a quick follow up. In my controller, I'm
trying to create order line items, but I'm getting stuck at a very
basic place. How do I add my line item object to my order object?
Here's what I have so far ...

               @ec_order = EcOrder.new(params[:ec_order])
               for form_item_id in params[:ec_order][:form_items]
                       @form_item = FormItem.find(form_item_id)
                       EcLineItem.new(:form_item_id => form_item_id)
                       # How do I add line item to order?
               end

assuming EcOrder has_many :ec_line_items, it's as easy as

@ec_order.ec_line_items.build :form_item_id => form_item_id (which
won't save the new EcLineItem) or
@ec_order.ec_line_items.create :form_item_id => form_item_id (which
will)

or if you really want to do it all by hand
line_item = EcLineItem.new(:form_item_id => form_item_id)
@ec_order.ec_line_items << line_item

Fred