# How do I build this form?

**URL:** <https://www.ruby-forum.com/t/how-do-i-build-this-form/144871>\
**Category:** Rails\
**Created:** [August 3, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871 "2008-08-03T17:26:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 3, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871/1 "2008-08-03T17:26:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 3, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871/2 "2008-08-03T18:41:14Z")

</div>

On 3 Aug 2008, at 16:25, removed\_email\_address@domain.invalid wrote:

> compile the below …

That makes sense - you haven’t defined ec\_line\_item anywhere. What is  
it supposed to be ?

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 3, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871/3 "2008-08-03T20:10:49Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 3, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871/4 "2008-08-03T21:35:09Z")

</div>

On 3 Aug 2008, at 19:09, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 3, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871/5 "2008-08-03T22:58:52Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 4, 2008, 1:21am UTC](https://www.ruby-forum.com/t/how-do-i-build-this-form/144871/6 "2008-08-04T01:21:32Z")

</div>

On 3 Aug 2008, at 21:58, removed\_email\_address@domain.invalid 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

>
