Ticket #7618: changed_form_helper_documentation.diff
| File changed_form_helper_documentation.diff, 7.7 kB (added by fearoffish, 3 years ago) |
|---|
-
lib/action_view/helpers/form_helper.rb
old new 4 4 5 5 module ActionView 6 6 module Helpers 7 # Provides a set of methods for working with forms and especially forms related to objects assigned to the template. 8 # The following is an example of a complete form for a person object that works for both creates and updates built 9 # with all the form helpers. The <tt>@person</tt> object was assigned by an action on the controller: 10 # <form action="save_person" method="post"> 11 # Name: 12 # <%= text_field "person", "name", "size" => 20 %> 7 # Form helpers are designed to make working with models much easier than just standard html elements. These helpers 8 # provide a set of methods for creating forms based on your models. Each helper deals with a different type of data. 9 # Instead of creating the html elements manually, you ask the helpers to create the form element. When the form is 10 # submitted i.e. when the user hits the submit button, the form elements will be bundled into the params object and 11 # passed back to the controller. 13 12 # 14 # Password:15 # <%= password_field "person", "password", "maxsize" => 20 %>13 # There are two types of form helper, those that specifically work with the attributes on models, and those that don't. 14 # First, an example of a form generated for a login page that doesn't deal with model attributes: 16 15 # 17 # Single?: 18 # <%= check_box "person", "single" %> 16 # <% form_tag :controller => 'sessions', :action => 'new' do -%> 17 # <%= text_field_tag 'login' %> 18 # <%= password_field_tag 'password' %> 19 # 20 # <%= submit_tag 'Log in' %> 21 # <% end -%> 19 22 # 20 # Description: 21 # <%= text_area "person", "description", "cols" => 20 %> 23 # This would generate the following html: 22 24 # 23 # <input type="submit" value="Save"> 24 # </form> 25 # <form action="/sessions/new" method="post"> 26 # <input id="login" name="login" type="text" /> 27 # <input id="password" name="password" type="password" /> 28 # 29 # <input name="commit" type="submit" value="Log in" /> 30 # </form> 25 31 # 26 # ...is compiled to:32 # The params object created for this would look like: 27 33 # 28 # <form action="save_person" method="post"> 29 # Name: 30 # <input type="text" id="person_name" name="person[name]" 31 # size="20" value="<%= @person.name %>" /> 34 # {"commit"=>"Log in", "action"=>"create", "controller"=>"sessions", "login"=>"some_user", "password"=>"some_pass"} 32 35 # 33 # Password: 34 # <input type="password" id="person_password" name="person[password]" 35 # size="20" maxsize="20" value="<%= @person.password %>" /> 36 # Note how the params are not nested when creating a form this way. 36 37 # 37 # Single?: 38 # <input type="checkbox" id="person_single" name="person[single]" value="1" /> 38 # An example that specifically deals with a person object: 39 39 # 40 # Description: 41 # <textarea cols="20" rows="40" id="person_description" name="person[description]"> 42 # <%= @person.description %> 43 # </textarea> 40 # # Note: a @person variable will have been created in the controller and populated with data 41 # # e.g. @person = Person.find(1) 42 # <% form_for :person, @person, :url => { :action => "update" } do |f| %> 43 # <%= f.text_field :first_name %> 44 # <%= f.text_field :last_name %> 45 # <%= submit_tag 'Update' %> 46 # <% end %> 44 47 # 45 # <input type="submit" value="Save"> 46 # </form> 48 # The html generated for this would be: 47 49 # 50 # <form action="/persons/update" method="post"> 51 # <input id="person_first_name" name="person[first_name]" size="30" type="text" /> 52 # <input id="person_last_name" name="person[last_name]" size="30" type="text" /> 53 # <input name="commit" type="submit" value="Update" /> 54 # </form> 55 # 56 # The params object created when this form is submitted would look like: 57 # 58 # {"action"=>"create", "controller"=>"sessions", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}} 59 # 60 # The form_for method generates a form based on a method, in our example if the @person object had contained any 61 # values they would have been set in the form (this is how edit forms are created). Notice how the params hash 62 # has a nested 'person' value, which can therefore be accessed with params[:person] in the controller. 63 # 48 64 # If the object name contains square brackets the id for the object will be inserted. Example: 49 65 # 50 66 # <%= text_field "person[]", "name" %> … … 62 78 # 63 79 # <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" /> 64 80 # 65 # There 'salso methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,81 # There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html, 66 82 # link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html 67 83 module FormHelper 68 84 # Creates a form and a scope around a specific model object, which is then used as a base for questioning about … … 75 91 # Admin? : <%= f.check_box :admin %> 76 92 # <% end %> 77 93 # 78 # 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>,79 # not <tt><%= %></tt>. Also worth noting is that theform_for yields a form_builder object, in this example as f, which emulates94 # 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>, 95 # not <tt><%= %></tt>. Also worth noting is that form_for yields a form_builder object, in this example as f, which emulates 80 96 # the API for the stand-alone FormHelper methods, but without the object name. So instead of <tt>text_field :person, :name</tt>, 81 97 # you get away with <tt>f.text_field :name</tt>. 82 98 # … … 153 169 154 170 # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object 155 171 # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 156 # hash with +options+. 172 # hash with +options+. These options will be tagged onto the html as an html element attribute as in the example 173 # shown. 157 174 # 158 175 # Examples (call, result): 159 # text_field( "post", "title", "size"=> 20)176 # text_field(:post, :title, :size => 20) 160 177 # <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" /> 161 178 def text_field(object_name, method, options = {}) 162 179 InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("text", options) … … 202 219 # <input type="checkbox" id="post_validate" name="post[validated]" value="1" checked="checked" /> 203 220 # <input name="post[validated]" type="hidden" value="0" /> 204 221 # 205 # Example (call, result). Imagine that @puppy.gooddog returns no:222 # Example (call, result). Imagine that @puppy.gooddog returns "no": 206 223 # check_box("puppy", "gooddog", {}, "yes", "no") 207 224 # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" /> 208 225 # <input name="puppy[gooddog]" type="hidden" value="no" />