1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Doc fixes

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6234 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2007-02-25 20:19:24 +00:00
parent 9b46f69381
commit cd4dbd8cd2

View file

@ -4,47 +4,63 @@ require 'action_view/helpers/tag_helper'
module ActionView module ActionView
module Helpers module Helpers
# Provides a set of methods for working with forms and especially forms related to objects assigned to the template. # Form helpers are designed to make working with models much easier than just standard html elements. These helpers
# The following is an example of a complete form for a person object that works for both creates and updates built # provide a set of methods for creating forms based on your models. Each helper deals with a different type of data.
# with all the form helpers. The <tt>@person</tt> object was assigned by an action on the controller: # Instead of creating the html elements manually, you ask the helpers to create the form element. When the form is
# <form action="save_person" method="post"> # submitted i.e. when the user hits the submit button, the form elements will be bundled into the params object and
# Name: # passed back to the controller.
# <%= text_field "person", "name", "size" => 20 %>
# #
# Password: # There are two types of form helper, those that specifically work with the attributes on models, and those that don't.
# <%= password_field "person", "password", "maxsize" => 20 %> # First, an example of a form generated for a login page that doesn't deal with model attributes:
# #
# Single?: # <% form_tag :controller => 'sessions', :action => 'new' do -%>
# <%= check_box "person", "single" %> # <%= text_field_tag 'login' %>
# <%= password_field_tag 'password' %>
# #
# Description: # <%= submit_tag 'Log in' %>
# <%= text_area "person", "description", "cols" => 20 %> # <% end -%>
# #
# <input type="submit" value="Save"> # This would generate the following html:
#
# <form action="/sessions/new" method="post">
# <input id="login" name="login" type="text" />
# <input id="password" name="password" type="password" />
#
# <input name="commit" type="submit" value="Log in" />
# </form> # </form>
# #
# ...is compiled to: # The params object created for this would look like:
# #
# <form action="save_person" method="post"> # {"commit"=>"Log in", "action"=>"create", "controller"=>"sessions", "login"=>"some_user", "password"=>"some_pass"}
# Name:
# <input type="text" id="person_name" name="person[name]"
# size="20" value="<%= @person.name %>" />
# #
# Password: # Note how the params are not nested when creating a form this way.
# <input type="password" id="person_password" name="person[password]"
# size="20" maxsize="20" value="<%= @person.password %>" />
# #
# Single?: # An example that specifically deals with a person object:
# <input type="checkbox" id="person_single" name="person[single]" value="1" />
# #
# Description: # # Note: a @person variable will have been created in the controller and populated with data
# <textarea cols="20" rows="40" id="person_description" name="person[description]"> # # e.g. @person = Person.find(1)
# <%= @person.description %> # <% form_for :person, @person, :url => { :action => "update" } do |f| %>
# </textarea> # <%= f.text_field :first_name %>
# <%= f.text_field :last_name %>
# <%= submit_tag 'Update' %>
# <% end %>
# #
# <input type="submit" value="Save"> # The html generated for this would be:
#
# <form action="/persons/update" method="post">
# <input id="person_first_name" name="person[first_name]" size="30" type="text" />
# <input id="person_last_name" name="person[last_name]" size="30" type="text" />
# <input name="commit" type="submit" value="Update" />
# </form> # </form>
# #
# The params object created when this form is submitted would look like:
#
# {"action"=>"create", "controller"=>"sessions", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
#
# The form_for method generates a form based on a method, in our example if the @person object had contained any
# values they would have been set in the form (this is how edit forms are created). Notice how the params hash
# has a nested 'person' value, which can therefore be accessed with params[:person] in the controller.
#
# If the object name contains square brackets the id for the object will be inserted. Example: # If the object name contains square brackets the id for the object will be inserted. Example:
# #
# <%= text_field "person[]", "name" %> # <%= text_field "person[]", "name" %>
@ -62,7 +78,7 @@ module ActionView
# #
# <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" /> # <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
# #
# There's also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html, # There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
# link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html # link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
module FormHelper module FormHelper
# Creates a form and a scope around a specific model object, which is then used as a base for questioning about # Creates a form and a scope around a specific model object, which is then used as a base for questioning about
@ -75,8 +91,8 @@ module ActionView
# Admin? : <%= f.check_box :admin %> # Admin? : <%= f.check_box :admin %>
# <% end %> # <% end %>
# #
# Worth noting is that the form_for tag is called in a ERb evaluation block, not a ERb output block. So that's <tt><% %></tt>, # Worth noting is that the form_for tag is called in a ERb evaluation block, not an ERb output block. So that's <tt><% %></tt>,
# not <tt><%= %></tt>. Also worth noting is that the form_for yields a form_builder object, in this example as f, which emulates # not <tt><%= %></tt>. Also worth noting is that form_for yields a form_builder object, in this example as f, which emulates
# the API for the stand-alone FormHelper methods, but without the object name. So instead of <tt>text_field :person, :name</tt>, # the API for the stand-alone FormHelper methods, but without the object name. So instead of <tt>text_field :person, :name</tt>,
# you get away with <tt>f.text_field :name</tt>. # you get away with <tt>f.text_field :name</tt>.
# #
@ -153,10 +169,11 @@ module ActionView
# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. # hash with +options+. These options will be tagged onto the html as an html element attribute as in the example
# shown.
# #
# Examples (call, result): # Examples (call, result):
# text_field("post", "title", "size" => 20) # text_field(:post, :title, :size => 20)
# <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" /> # <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />
def text_field(object_name, method, options = {}) def text_field(object_name, method, options = {})
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("text", options) InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("text", options)
@ -202,7 +219,7 @@ module ActionView
# <input type="checkbox" id="post_validate" name="post[validated]" value="1" checked="checked" /> # <input type="checkbox" id="post_validate" name="post[validated]" value="1" checked="checked" />
# <input name="post[validated]" type="hidden" value="0" /> # <input name="post[validated]" type="hidden" value="0" />
# #
# Example (call, result). Imagine that @puppy.gooddog returns no: # Example (call, result). Imagine that @puppy.gooddog returns "no":
# check_box("puppy", "gooddog", {}, "yes", "no") # check_box("puppy", "gooddog", {}, "yes", "no")
# <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" /> # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
# <input name="puppy[gooddog]" type="hidden" value="no" /> # <input name="puppy[gooddog]" type="hidden" value="no" />