NetManiac

Witold Rugowski on web20 wave with Ruby on Rails

form_tag and HTML options

Posted on November 6, 2006 - Filed Under RubyOnRails

Many people are complaining about Rails and Ruby documentation (well, read what WhyTheLuckyStiff has to say about DjangoBook, and all comments there). I agree. Let’s take form_tag in it’s new form (for 1.2 release). From official documentation (it is hard to give link, since it is to some frame, and may change, so if this not lead to form_tag don’t blame me ;-) ), even if this is probably for old form (without do block) it lacks info about options:

Starts a form tag that points the action to an url configured with url_for_options just like ActionController::Base#url_for. The method for the form defaults to POST.

Options:

* :multipart – If set to true, the enctype is set to “multipart/form-data”.
* :method – The method to use when submitting the form, usually either “get” or “post”.

This method is also aliased as start_form_tag

For me it suggest, there are only two options :multipart and :method. Wrong guess. Look at code (show source is really useful):

 # File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 18
18:       def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc)
19:         html_options = { "method" => "post" }.merge(options.stringify_keys)
20:         html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
21:         html_options["action"] = url_for(url_for_options, *parameters_for_url)
22:         tag :form, html_options, true
23:       end

As we can se in line 19 all options passed as a hash are append to generated HTML code. So if we need to use custom style for form:

<%  form_tag( {:action => "save"},
    {:id => "routeForm", :method => "post", :style => "margin-top: 10px" } )  do -%>
SOME_HTML_OR_FIELD_HELPERS
<% end %>

Generates:

<form action="/route/save" id="routeForm" method="post"
style="margin-top: 10px">

Popularity: 6% [?]

Hits for this post: 6452

Similar Posts

Comments

Leave a Reply