From 66b88129cc9f9169881aca46c1087d0901093144 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Wed, 31 Dec 2008 12:33:12 +0000 Subject: Tidy up discussion of second hash passed to form_tag/html options for form_for --- railties/doc/guides/source/form_helpers.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'railties') diff --git a/railties/doc/guides/source/form_helpers.txt b/railties/doc/guides/source/form_helpers.txt index a263f94376..e4584392e3 100644 --- a/railties/doc/guides/source/form_helpers.txt +++ b/railties/doc/guides/source/form_helpers.txt @@ -92,22 +92,22 @@ TIP: For every form input, an ID attribute is generated from its name ("q" in ou Multiple hashes in form helper attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -By now we've seen that the `form_tag` helper accepts 2 arguments: the path for the action attribute and an options hash for parameters (like `:method`). +By now we've seen that the `form_tag` helper accepts 2 arguments: the path for the action attribute and an options hash. This hash specifies the method of form submission and html level options such as the form element's class. Identical to the `link_to` helper, the path argument doesn't have to be given as string or a named route. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, we cannot simply write this: .A bad way to pass multiple hashes as method arguments ---------------------------------------------------------------------------- -form_tag(:controller => "people", :action => "search", :method => "get") -# =>
+form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form") +# => ---------------------------------------------------------------------------- Here we wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL that we didn't want. The solution is to delimit the first hash (or both hashes) with curly brackets: .The correct way of passing multiple hashes as arguments ---------------------------------------------------------------------------- -form_tag({:controller => "people", :action => "search"}, :method => "get") -# => +form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form") +# => ---------------------------------------------------------------------------- This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly. @@ -249,7 +249,7 @@ Now we switch to the view. The first thing to remember is to use the `form_for` .articles/new.html.erb ---------------------------------------------------------------------------- -<% form_for :article, @article, :url => { :action => "create" } do |f| %> +<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> <%= f.text_field :title %> <%= f.text_area :body, :size => "60x12" %> <%= submit_tag "Create" %> @@ -259,14 +259,14 @@ Now we switch to the view. The first thing to remember is to use the `form_for` There are a few things to note here: 1. `:article` is the name of the model and `@article` is our record. -2. The URL for the action attribute is passed as a parameter named `:url`. +2. There is a single hash of options. Routing options are passed inside `:url` hash, html options are passed in the `:html` hash. 3. The `form_for` method yields *a form builder* object (the `f` variable). 4. Methods to create form controls are called *on* the form builder object `f` The resulting HTML is: ---------------------------------------------------------------------------- - + -- cgit v1.2.3