<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sean&#039;s Blog &#187; template</title>
	<atom:link href="http://seanbehan.com/tag/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Tue, 31 Aug 2010 14:38:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Extending Rails Form Builders</title>
		<link>http://seanbehan.com/ruby-on-rails/extending-rails-form-builders/</link>
		<comments>http://seanbehan.com/ruby-on-rails/extending-rails-form-builders/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 19:11:22 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[builders]]></category>
		<category><![CDATA[custom tag builder]]></category>
		<category><![CDATA[extending]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[label]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[text field]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=825</guid>
		<description><![CDATA[Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so that I can display a more human friendly [...]]]></description>
			<content:encoded><![CDATA[<p>Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so that I can display a more human friendly string to represent the form field.</p>
<pre>
# RAILS_ROOT/app/helpers/custom_tag_builder.rb
class CustomTagBuilder < ActionView::Helpers::FormBuilder

  def self.create_tagged_field(method_name)
    define_method(method_name) do |label, *args|
      label_name = args.first.blank? ? label : args.first[:label] # my change
      @template.content_tag("p",
        @template.content_tag("label",
          label_name.to_s.humanize, :for => "#{@object_name}_#{label}") +" <br/> "+ super)
    end
  end

  field_helpers.each do |name|
    create_tagged_field(name)
  end
end
</pre>
<p>You can then use this in your views</p>
<pre>
form_for @your_model, :builder => CustomTagBuilder do |f|
  f.text_field :fullname
  f.text_field :email, :label => "Email (will not be published)"
</pre>
<p>My change tests for the presence of a label argument otherwise using the name of the form field/model attribute. In this case the fullname attribute will be outputted as &#8220;Fullname&#8221; while &#8220;Email (will not be published)&#8221; as the label for the email text field.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/extending-rails-form-builders/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
