<?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; text field</title>
	<atom:link href="http://seanbehan.com/tag/text-field/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>Wed, 18 Jan 2012 21:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.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[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fextending-rails-form-builders%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Extending Rails Form Builders" data-url="http://seanbehan.com/ruby-on-rails/extending-rails-form-builders/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><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 class="wp-code-highlight prettyprint">
# RAILS_ROOT/app/helpers/custom_tag_builder.rb
class CustomTagBuilder &lt; 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(&quot;p&quot;,
        @template.content_tag(&quot;label&quot;,
          label_name.to_s.humanize, :for =&gt; &quot;#{@object_name}_#{label}&quot;) +&quot; &lt;br/&gt; &quot;+ 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 class="wp-code-highlight prettyprint">
form_for @your_model, :builder =&gt; CustomTagBuilder do |f|
  f.text_field :fullname
  f.text_field :email, :label =&gt; &quot;Email (will not be published)&quot;
</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>

