Form Helpers

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]

Name
Email
Telephone
Message
[/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 :frowning: 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

Anyone know how to use code tags???

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs