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 %>

Description
<%= form_item.description %>

<%= ec_line_item.check_box(:quantity, '', {}, "Request Item", '') %>
<% 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

On 3 Aug 2008, at 16:25, [email protected] wrote:

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

On Aug 3, 11:40 am, Frederick C. [email protected]

On 3 Aug 2008, at 19:09, [email protected] wrote:

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

On Aug 3, 2:34 pm, Frederick C. [email protected]

On 3 Aug 2008, at 21:58, [email protected] wrote:

                   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