Ruby’s beauty is often attributed to its terseness. However, there are times when you need to be really explicit in order to coax it to do your bidding. For example, a Rails form helper needs a bunch of parantheses and curly braces in order to be a named element. Which makes sense, as :id
can represent a Rails (ActiveRecord) object ID or a DOM element ID. The Ruby punctuation removes such ambiguity.
<%= form_tag( {:controller => 'controller', :action => 'action', :id => 1}, {:id => 'test', :name => 'test'} )%>
This code then produces this HTML.
<form action="/controller/action/1" id="test" method="post" name="test">
Not using the punctuation results in the ID being gobbled up by the URL, or attributes being tacked on to the URL as query string values, or an oh so helpful compile error. Mmmm taste the sarcasm.
The only reference to this was found, not in the API, but buried in some Rails mailing list thread indexed by Google. I figured I’d preserve this tidbit for prosperity seeing how I couldn’t find it on another blog.
Note: Naming form_remote_tag
is done differently. After deciphering the syntax by looking at the source code for the helper, it turns out the key is the :html
option.
<%= form_remote_tag :url => {:controller => 'controller', :action => 'action', :id => 1}, :html => {:id => 'test', :name => 'test'} %>