Hi everyone!
I''m using the advice in ''Agile Web Development 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>
<dl class="form">
<%= form.text_field :telephone %>
<%= form.text_area :message %>
</dl>
<ul class="form">
</li>
<%= submit_tag ''Send Message'' %>
</li>
</ul>
<% 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 Name</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]<form action="/contact/submit" method="post">
<dl class="form">
<dt><label
for="enquiry_name">Name</label></dt><dd><input
id="enquiry_name" name="enquiry[name]" size="30"
type="text" value=""
/></dd>
<dt><label
for="enquiry_email">Email</label></dt><dd><input
id="enquiry_email" name="enquiry[email]" size="30"
type="text" value=""
/></dd>
</dl>
<dl class="form">
<dt><label
for="enquiry_telephone">Telephone</label></dt><dd><input
id="enquiry_telephone" name="enquiry[telephone]"
size="30" type="text"
value="" /></dd>
<dt><label
for="enquiry_message">Message</label></dt><dd><textarea
cols="40" id="enquiry_message"
name="enquiry[message]"
rows="20"></textarea></dd>
</dl>
<ul class="form">
<li><input name="commit" type="submit"
value="Send Message" /></li>
</ul>
</form>[/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]<dt><label for="name">Your Name</label>
<em>(required)</em>:</dd>
<dd><input type="text" name="name"
value="James Hargreaves"
/></dt>[/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
Attachments:
http://www.ruby-forum.com/attachment/152/form.png
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---