# Showing a Form

**URL:** <https://www.ruby-forum.com/t/showing-a-form/149253>\
**Category:** Rails\
**Created:** [September 22, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/showing-a-form/149253 "2008-09-22T23:24:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [September 22, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/showing-a-form/149253/1 "2008-09-22T23:24:52Z")

</div>

Hi,  
Thanks to Erol for giving me input on how to move stuff to a namespace.  
It  
meant some work, so next time I will spend more time thinking before  
blasting off.

I’ve stored a Form in my DB which has Elements. Below is my  
FormsController.  
All it does is show.

class FormsController \< ApplicationController  
def show  
@form = Form.find(params[:id])  
@elements = Element.find(:all, :conditions =\> { :form\_id =\> @form })

```
respond_to do |format|
  format.html # show.html.erb
end

```

end

end

I get stuck creating the view. It should go through all of the Form’s  
elements and display a label with the Element’s name, and a text field  
with  
the Element’s value.

Right now I have this:

\<% form\_for @form, :url =\> { :action =\> ‘create’ } do |f| %\>

 \<%= @form.name %\> \<% form\_for(@form) do |f| %\> \<% for element in @elements %\> \<%= f.label\_tag 'value', 'name' %\> \<%= f.text\_field\_tag 'value', element.value %\> \<% end %\> \<%= f.submit 'Submit' %\> \<% end %\> \<% end %\>

I get this error:

undefined method `label\_tag’ for  
#ActionView::Helpers::FormBuilder:0xbcd7ee

---

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [September 23, 2008, 9:42am UTC](https://www.ruby-forum.com/t/showing-a-form/149253/2 "2008-09-23T09:42:27Z")

</div>

Whenever you’re using form\_for(anObject), you want to use FormHelpers:

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

Whenever you’re using a standalone form that’s not tied to a model’s  
attributes (IE, one in which you do not pass the FormConstructor into  
the block as ‘f’, on which you’d call f.text\_field ) you want to use  
FormTagHelpers:

> **[ActionView::Helpers::FormTagHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html)**
>
> Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.

You’re using form\_for with an object, which means you should use the  
‘tagless’ versions of the form constructors, but instead you’re using  
f.text\_field\_tag. The correct syntax is:

f.text\_field :attribute\_name, {value =\> element.value}

However, since you’re linking up associations in your form you’ll want  
to use the fields\_for helper, to bring the constructor into scope  
while still referring to the proper element in the form. Read about it  
here:

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001741)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

Now, since you can set up Forms to ‘has\_many’ Elements, and Elements  
‘belong\_to’ a Form, you can make your query like this in your  
Controller:

@form = Form.find(params[:id], {:include =\> :elements})

And then have the following code in your controller (Or something like  
it, play with it a bit, I’ve never done anything exactly like this  
before) :

\<%= form\_for @form, :action =\> create do |f| %\>  
\<% @form.elements.each do |element| %\>  
\<% fields\_for element do |e| %\>  
\<%= e.label ‘value’ %\>  
\<%= e.text\_field :value %\>  
\<% end %\>  
\<% end %\>  
\<%= submit\_tag %\>  
\<% end %\>

Play with the params output of that, and adjust your create action  
accordingly.

If that’s not pretty enough for you, use HAML:

= form\_for @form, :action =\> create do |f|

- @form.elemenst.each do |element|
  - fields\_for element do |e|  
= e.label ‘value’  
= e.text\_field :value  
= submit\_tag

On Sep 22, 5:23 pm, “Commander J.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
