# Form Helpers

**URL:** <https://www.ruby-forum.com/t/form-helpers/112804>\
**Category:** Rails\
**Created:** [August 23, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/form-helpers/112804 "2007-08-23T12:53:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [August 23, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/form-helpers/112804/1 "2007-08-23T12:53:12Z")

</div>

Hi everyone!

I’m using the advice in ‘Agile Web D. With Rails’ and creating  
a FormBuilder sub-class to handle the formatting of elements in my  
standard forms. Hence, I have the following view:

[code]\<%= error\_messages\_for :enquiry -%\>

\<% tagged\_form\_for :enquiry,  
:url =\> { :action =\> “submit” } do |form| %\>

```
    <dl class="form">

```

\<%= form.text\_field :name %\>  
\<%= form.text\_field :email %\>

```
    <dl class="form">

```

\<%= form.text\_field :telephone %\>  
\<%= form.text\_area :message %\>

```
    <ul class="form">
      </li>

```

\<%= submit\_tag ‘Send Message’ %\>

\<% end %\>[/code]

Which uses the following code in application\_helper.rb:

[code]# Methods added to this helper will be available to all templates  
in the application.  
module ApplicationHelper  
def tagged\_form\_for(name, \*args, &block)  
options = args.last.is\_a?(Hash) ? args.pop : {}  
options = options.merge(:builder =\> TaggedFormBuilder)  
args = (args \<\< options)  
form\_for(name, \*args, &block)  
end

private

class TaggedFormBuilder \< ActionView::Helpers::FormBuilder

```
# <dt><label for="name">Your N.</label> <em>(required)</em>:</dd>
# <dd><%= form.text_field :name %></dd>

def self.create_tagged_field(method_name)
  define_method(method_name) do |label, *args|
    @template.content_tag("dt",
                          @template.content_tag("label",
                                                label.to_s.humanize,
                                                :for =>

```

“#{@object\_name}\_#{label}”)) +  
@template.content\_tag(“dd”,  
super)  
end  
end

```
field_helpers.each do |name|
  create_tagged_field(name)
end

```

end  
end[/code]

And that results in the following HTML:

[code]

NameEmailTelephoneMessage
- 

[/code]

Which can be seen in the attached screenshot.

So, all very nice so far. However, I think it can be made better, for  
instance, the form is clearly split into sections so it would be nice to  
have the view say something like:

[code]\<% tagged\_form\_for :enquiry,  
:url =\> { :action =\> “submit” } do |form| %\>

\<% form.section do |section| %\>  
\<%= section.text\_field :name %\>  
\<%= section.text\_field :email %\>  
\<% end %\>

\<% form.section do |section| %\>  
\<%= section.text\_field :telephone %\>  
\<%= section.text\_field :message %\>  
\<% end %\>

…

\<% end %\>[/code]

But I can’t figure out how to do this ☹ It would also be nice to have a  
specific command to generate the submit section of the form:

[code]\<% tagged\_form\_for :enquiry,  
:url =\> { :action =\> “submit” } do |form| %\>

…

\<% form.submit\_section %\>

\<% end %\>[/code]

But again, this is beyond my feeble grasp of RoR. Finally, I need to add  
a :required =\> true|false parameter to the options for each field, which  
will flag the field in the HTML output as follows:

[code]

Your N. _(required)_:[/code]

I think this change should (probably) be the easiest of the lot, but it  
is still beyond my abilities.

Anyway, help with ANY of these issues would be appreciated.

Thanks  
Jay

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [August 23, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/form-helpers/112804/2 "2007-08-23T13:48:04Z")

</div>

Anyone know how to use _code_ tags???

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [August 23, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/form-helpers/112804/3 "2007-08-23T13:47:37Z")

</div>

Sorry, bad use of _code_ tags. Anyway, I’ve done the ‘required’ bit,  
which I said I thought would be the easiest bit:

` def self.create_tagged_field(method_name)
      define_method(method_name) do |label, *args|
        options = args.last.is_a?(Hash) ? args.pop : {}
        required = options[:required] ? @template.content_tag("em", " 
(required)") : ''
        @template.content_tag("dt",
                              @template.content_tag("label",
                                                    label.to_s.humanize,
                                                    :for => 
"#{@object_name}_#{label}") +
                              required) +
        @template.content_tag("dd",
                              super)
      end
    end`

So that just uses the :required option to determine whether a required  
flag should be shown.

However, I’m still stuck with the other stuff. Any help would be greatly  
appreciated.

Thanks  
Jay
