<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: The button_to helper is now part of Rails!</title>
    <link>http://blog.moertel.com/articles/2005/06/16/the-button_to-helper-is-now-part-of-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>The button_to helper is now part of Rails!</title>
      <description>&lt;p&gt;I am delighted to report that the &lt;a href="http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails"&gt;button_to
helper&lt;/a&gt;
has been added to the &lt;a href="http://www.rubyonrails.com/"&gt;Ruby on Rails&lt;/a&gt;
web-development framework.  &lt;a href="http://www.loudthinking.com/"&gt;David&lt;/a&gt;
applied &lt;a href="http://dev.rubyonrails.org/changeset/1437"&gt;the patch&lt;/a&gt; earlier
today, and so &lt;em&gt;button_to&lt;/em&gt; will be in the much-anticipated Rails 1.0
release.&lt;/p&gt;


	&lt;p&gt;David&amp;#8217;s change-log entry summarizes the patch well:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Added &lt;em&gt;button_to&lt;/em&gt; as a form-based solution to deal with harmful
actions that should be hidden behind POSTs. This makes it just as
easy as &lt;em&gt;link_to&lt;/em&gt; to create a safe trigger for actions like destroy,
although it&amp;#8217;s limited by being a block element, the fixed look,
and a no-no inside other forms.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;David does a good job of highlighting the helper&amp;#8217;s limitations. I&amp;#8217;ll
take this opportunity to elaborate on each.&lt;/p&gt;


	&lt;h3&gt;It is a block element&lt;/h3&gt;


	&lt;p&gt;The &lt;em&gt;button_to&lt;/em&gt; helper creates a small form, which in &lt;span class="caps"&gt;HTML&lt;/span&gt; is considered
&lt;a href="http://www.w3.org/TR/html4/sgml/dtd.html#block"&gt;block content&lt;/a&gt;, just
like the &lt;em&gt;p&lt;/em&gt;, &lt;em&gt;div&lt;/em&gt;, and &lt;em&gt;blockquote&lt;/em&gt; elements are. Basically, block
content cannot be mixed into runs of text. But links can: links are
&lt;a href="http://www.w3.org/TR/html4/sgml/dtd.html#inline"&gt;inline content&lt;/a&gt;. Thus
&lt;em&gt;button_to&lt;/em&gt; cannot be used as a drop-in replacement for every
occurrence of &lt;em&gt;link_to&lt;/em&gt; that might be unsafe; it works only for those
occurrences within block-accepting contexts.&lt;/p&gt;


	&lt;p&gt;Luckily for us, when designers use links to trigger unsafe actions,
they rarely slip such links into the middle of ordinary looking
text. Naughty uses of &lt;em&gt;link_to&lt;/em&gt; almost always occur within contexts
that accept block content. In Rails-generated scaffolding code, for
instance, the unsafe uses of &lt;em&gt;link_to&lt;/em&gt; occur within table cells, and
table cells have a &lt;a href="http://www.w3.org/TR/html4/sgml/dtd.html#flow"&gt;flow content
model&lt;/a&gt;, which accepts
both inline and block content. So &lt;em&gt;button_to&lt;/em&gt; works great for the
default cases in Rails.&lt;/p&gt;


	&lt;h3&gt;It has a fixed look&lt;/h3&gt;


	&lt;p&gt;As its name implies, &lt;em&gt;button_to&lt;/em&gt; creates buttons. Buttons don&amp;#8217;t look
like links and aren&amp;#8217;t styled the same way that links are. For some
design scenarios, this might be a problem.&lt;/p&gt;


	&lt;p&gt;(My view is that links should not be used to trigger unsafe
actions. In the same way that action-triggering &lt;span class="caps"&gt;GET&lt;/span&gt; requests violate
the spirit of the &lt;span class="caps"&gt;HTTP&lt;/span&gt; standards, action-triggering hypertext links
violate the spirit of the &lt;span class="caps"&gt;HTML&lt;/span&gt; standards. For this reason, I view this
limitation as a feature.)&lt;/p&gt;


	&lt;h3&gt;It is a no-no inside other forms&lt;/h3&gt;


	&lt;p&gt;Forms cannot be nested, and so &lt;em&gt;button_to&lt;/em&gt; cannot be used inside of
forms.&lt;/p&gt;


	&lt;p&gt;Fortunately, this limitation usually doesn&amp;#8217;t matter because when we
are inside of a form, we can use its buttons instead of
&lt;em&gt;button_to&lt;/em&gt;-created buttons to trigger actions. Still, there are some
circumstances where it does matter, such as the &amp;#8220;Amazon.com wish list&amp;#8221; 
scenario. In this scenario, we should consider &lt;a href="http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails#comment-110"&gt;other
options&lt;/a&gt;.&lt;/p&gt;


	&lt;h3&gt;The bottom line: Pick the low-hanging fruit&lt;/h3&gt;


	&lt;p&gt;While &lt;em&gt;button_to&lt;/em&gt; has its limitations, it does provide a simple solution
to the unsafe-GET problem for most real-world cases. I am glad that it
is now a part of Rails, and I offer a big thank-you to David for
accepting the patch.&lt;/p&gt;</description>
      <pubDate>Thu, 16 Jun 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:29f013421357d69f430f725d21062807</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/06/16/the-button_to-helper-is-now-part-of-rails</link>
      <category>web development</category>
      <category>rails</category>
      <category>rails</category>
      <category>get</category>
      <category>gwa</category>
      <category>safe</category>
      <category>unsafe</category>
      <category>post</category>
      <category>link_to</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/46</trackback:ping>
    </item>
  </channel>
</rss>
