Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8080

Show
Ignore:
Timestamp:
11/06/07 05:56:02 (1 year ago)
Author:
nzkoz
Message:

Grammatical and whitespace fixes for form helper docs. Closes #10062 [chuyeow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_view/helpers/form_helper.rb

    r8033 r8080  
    55module ActionView 
    66  module Helpers 
    7     # Form helpers are designed to make working with models much easier than just standard html elements by 
    8     # providing a set of methods for creating forms based on your models.  This helper generates the HTML for forms, 
    9     # providing a method for each sort of input (e.g., text, password, select, and so on).  When the form is  
    10     # submitted (i.e., when the user hits the submit button or <tt>form.submit</tt> is called via JavaScript), the form  
    11     # inputs will be bundled into the <tt>params</tt> object and passed back to the controller. 
    12     # 
    13     # There are two types of form helpers: those that specifically work with model attributes and those that don't.   
     7    # Form helpers are designed to make working with models much easier compared to using just standard HTML 
     8    # elements by providing a set of methods for creating forms based on your models. This helper generates the HTML 
     9    # for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form 
     10    # is submitted (i.e., when the user hits the submit button or <tt>form.submit</tt> is called via JavaScript), the form inputs will be bundled into the <tt>params</tt> object and passed back to the controller. 
     11    # 
     12    # There are two types of form helpers: those that specifically work with model attributes and those that don't. 
    1413    # This helper deals with those that work with model attributes; to see an example of form helpers that don't work 
    1514    # with model attributes, check the ActionView::Helpers::FormTagHelper documentation. 
    1615    # 
    17     # The core method of this helper, form_for, gives you the ability to create a form for a model instance;  
     16    # The core method of this helper, form_for, gives you the ability to create a form for a model instance; 
    1817    # for example, let's say that you have a model <tt>Person</tt> and want to create a new instance of it: 
    1918    # 
    20     #     # Note: a @person variable will have been created in the controller.  
     19    #     # Note: a @person variable will have been created in the controller. 
    2120    #     # For example: @person = Person.new 
    2221    #     <% form_for :person, @person, :url => { :action => "create" } do |f| %> 
     
    3736    # 
    3837    #     {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}} 
    39     #  
     38    # 
    4039    # The params hash has a nested <tt>person</tt> value, which can therefore be accessed with <tt>params[:person]</tt> in the controller. 
    4140    # If were editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than <tt>Person.new</tt> in the controller), the objects 
    4241    # attribute values are filled into the form (e.g., the <tt>person_first_name</tt> field would have that person's first name in it). 
    43     #     
     42    # 
    4443    # If the object name contains square brackets the id for the object will be inserted. For example: 
    4544    # 
    4645    #   <%= text_field "person[]", "name" %>  
    47     #  
     46    # 
    4847    # ...will generate the following ERb. 
    4948    # 
     
    6362    module FormHelper 
    6463      # Creates a form and a scope around a specific model object that is used as a base for questioning about 
    65       # values for the fields.   
     64      # values for the fields. 
    6665      # 
    6766      #   <% form_for :person, @person, :url => { :action => "update" } do |f| %> 
     
    7271      #   <% end %> 
    7372      # 
    74       # 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>,  
     73      # 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>, 
    7574      # not <tt><%= %></tt>. Also worth noting is that form_for yields a <tt>form_builder</tt> object, in this example as <tt>f</tt>, which emulates 
    7675      # the API for the stand-alone FormHelper methods, but without the object name. So instead of <tt>text_field :person, :name</tt>, 
    77       # you get away with <tt>f.text_field :name</tt>.  
    78       # 
    79       # Even further, the form_for method allows you to more easily escape the instance variable convention. So while the stand-alone  
    80       # approach would require <tt>text_field :person, :name, :object => person</tt>  
     76      # you get away with <tt>f.text_field :name</tt>. 
     77      # 
     78      # Even further, the form_for method allows you to more easily escape the instance variable convention. So while the stand-alone 
     79      # approach would require <tt>text_field :person, :name, :object => person</tt> 
    8180      # to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with  
    8281      # <tt>:person, person</tt> and all subsequent field calls save <tt>:person</tt> and <tt>:object => person</tt>. 
     
    9291      #   <% end %> 
    9392      # 
    94       # Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base. 
    95       # Like FormOptionHelper#collection_select and DateHelper#datetime_select. 
     93      # Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base, 
     94      # like FormOptionHelper#collection_select and DateHelper#datetime_select. 
    9695      # 
    9796      # HTML attributes for the form tag can be given as :html => {...}. For example: 
    98       #      
     97      # 
    9998      #   <% form_for :person, @person, :html => {:id => 'person_form'} do |f| %> 
    10099      #     ... 
     
    146145      # 
    147146      # You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers, 
    148       # then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs. 
    149       #    
     147      # then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs. 
     148      # 
    150149      #   <% form_for :person, @person, :url => { :action => "update" }, :builder => LabellingFormBuilder do |f| %> 
    151150      #     <%= f.text_field :first_name %> 
     
    154153      #     <%= check_box_tag "person[admin]", @person.company.admin? %> 
    155154      #   <% end %> 
    156       #  
     155      # 
    157156      # In many cases you will want to wrap the above in another helper, so you could do something like the following: 
    158157      # 
     
    189188      def apply_form_for_options!(object_or_array, options) #:nodoc: 
    190189        object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array 
    191          
     190 
    192191        html_options = 
    193192          if object.respond_to?(:new_record?) && object.new_record? 
     
    209208      #     First name: <%= person_form.text_field :first_name %> 
    210209      #     Last name : <%= person_form.text_field :last_name %> 
    211       #      
     210      # 
    212211      #     <% fields_for :permission, @person.permission do |permission_fields| %> 
    213212      #       Admin?  : <%= permission_fields.check_box :admin %> 
     
    229228      # assigned to the template (identified by +object+). The text of label will default to the attribute name unless you specify 
    230229      # it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged 
    231       # onto the html as an HTML element attribute as in the example shown. 
     230      # onto the HTML as an HTML element attribute as in the example shown. 
    232231      # 
    233232      # ==== Examples 
     
    247246      # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object 
    248247      # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 
    249       # hash with +options+. These options will be tagged onto the html as an HTML element attribute as in the example 
     248      # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example 
    250249      # shown. 
    251250      # 
     
    269268      # Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object 
    270269      # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 
    271       # hash with +options+. These options will be tagged onto the html as an HTML element attribute as in the example 
     270      # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example 
    272271      # shown. 
    273272      # 
     
    291290      # Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object 
    292291      # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 
    293       # hash with +options+. These options will be tagged onto the html as an html element attribute as in the example 
     292      # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example 
    294293      # shown. 
    295294      # 
     
    302301      # 
    303302      #   hidden_field(:user, :token) 
    304       #   # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />    
     303      #   # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" /> 
    305304      def hidden_field(object_name, method, options = {}) 
    306305        InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("hidden", options) 
     
    309308      # Returns an file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object 
    310309      # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a 
    311       # hash with +options+. These options will be tagged onto the html as an html element attribute as in the example 
     310      # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example 
    312311      # shown. 
    313312      # 
     
    457456        options["checked"]  = "checked" if checked 
    458457        pretty_tag_value    = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase 
    459         options["id"]     ||= defined?(@auto_index) ?              
     458        options["id"]     ||= defined?(@auto_index) ? 
    460459          "#{@object_name}_#{@auto_index}_#{@method_name}_#{pretty_tag_value}" : 
    461460          "#{@object_name}_#{@method_name}_#{pretty_tag_value}" 
     
    511510        tag_text << ">True</option></select>" 
    512511      end 
    513        
     512 
    514513      def to_content_tag(tag_name, options = {}) 
    515514        content_tag(tag_name, value(object), options) 
    516515      end 
    517        
     516 
    518517      def object 
    519518        @object || (@template_object.instance_variable_get("@#{@object_name}") rescue nil) 
     
    527526        self.class.value_before_type_cast(object, @method_name) 
    528527      end 
    529        
     528 
    530529      class << self 
    531530        def value(object, method_name) 
    532531          object.send method_name unless object.nil? 
    533532        end 
    534          
     533 
    535534        def value_before_type_cast(object, method_name) 
    536535          unless object.nil? 
     
    540539          end 
    541540        end 
    542          
     541 
    543542        def check_box_checked?(value, checked_value) 
    544543          case value 
     
    555554          end 
    556555        end 
    557          
     556 
    558557        def radio_button_checked?(value, checked_value) 
    559558          value.to_s == checked_value.to_s 
     
    605604 
    606605      def initialize(object_name, object, template, options, proc) 
    607         @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc         
    608       end 
    609        
     606        @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc 
     607      end 
     608 
    610609      (field_helpers - %w(label check_box radio_button fields_for)).each do |selector| 
    611610        src = <<-end_src 
     
    629628        @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) 
    630629      end 
    631        
     630 
    632631      def radio_button(method, tag_value, options = {}) 
    633632        @template.radio_button(@object_name, method, tag_value, options.merge(:object => @object)) 
    634633      end 
    635        
     634 
    636635      def error_message_on(method, prepend_text = "", append_text = "", css_class = "formError") 
    637636        @template.error_message_on(@object, method, prepend_text, append_text, css_class) 
    638       end       
     637      end 
    639638 
    640639      def error_messages(options = {}) 
    641640        @template.error_messages_for(@object_name, options.merge(:object => @object)) 
    642641      end 
    643        
     642 
    644643      def submit(value = "Save changes", options = {}) 
    645644        @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))