Skip to content
This repository
Browse code

describe foo_tag versus foo

  • Loading branch information...
commit 73ebb26ece2075e925c89a991b1e3c8ffabe84fd 1 parent d6c38f1
Frederick Cheung fcheung authored

Showing 1 changed file with 37 additions and 0 deletions. Show diff stats Hide diff stats

  1. +37 0 railties/doc/guides/source/form_helpers.txt
37 railties/doc/guides/source/form_helpers.txt
@@ -191,6 +191,43 @@ output:
191 191
192 192 When parsing POSTed data, Rails will take into account the special `"_method"` parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).
193 193
  194 +Different Families of helpers
  195 +------------------------------
  196 +
  197 +Most of Rails' form helpers are available in two forms.
  198 +
  199 +Barebones helpers
  200 +~~~~~~~~~~~~~~~~~~
  201 +These just generate the appropriate markup. These have names ending in _tag such as `text_field_tag`, check_box_tag`. The first parameter to these is always the name of the input. This is the name under which value will appear in the `params` hash in the controller. For example if the form contains
  202 +---------------------------
  203 +<%= text_field_tag(:query) %>
  204 +---------------------------
  205 +
  206 +then the controller code should use
  207 +---------------------------
  208 +params[:query]
  209 +---------------------------
  210 +to retrieve the value entered by the user.
  211 +
  212 +Model object helpers
  213 +~~~~~~~~~~~~~~~~~~~~~
  214 +These are designed to work with a model object (commonly an Active Record object but this need not be the case). These lack the _tag suffix, from example `text_field`, `text_area`
  215 +
  216 +For the latter the first parameter is the name of an instance variable and the second is the name of attribute to be edited. Rails will set the value of the input control to the current value of that attribute for the object and set an appropriate input name. If our controller has defined `@person` and that person's name is Henry then a form containing:
  217 +
  218 +---------------------------
  219 +<%= text_field(:person, :name) %>
  220 +---------------------------
  221 +will produce output similar to
  222 +---------------------------
  223 +<input id="person_name" name="person[name]" type="text" value="Henry"/>
  224 +---------------------------
  225 +The value entered by the user will be stored in
  226 +---------------------------
  227 +params[:person][:name]
  228 +---------------------------
  229 +The params[:person] hash is suitable for passing to `Person.new` or, if `@person` is an instance of Person, `@person.update_attributes`.
  230 +
194 231
195 232 Forms that deal with model attributes
196 233 -------------------------------------

0 comments on commit 73ebb26

Please sign in to comment.
Something went wrong with that request. Please try again.