form_tag_helper.rb 5.8 KB
Newer Older
1 2 3 4 5 6 7
require 'cgi'
require File.dirname(__FILE__) + '/tag_helper'

module ActionView
  module Helpers
    # Provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like
    # FormHelper does. With the FormTagHelper, you provide the names and values yourself.
8
    #
D
David Heinemeier Hansson 已提交
9
    # NOTE: The html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>:disabled => true</tt>
10
    # will give <tt>disabled="disabled"</tt>.
11
    module FormTagHelper
12
      # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
13 14 15 16
      # ActionController::Base#url_for. The method for the form defaults to POST.
      #
      # Options:
      # * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data".
17
      # * <tt>:method</tt> - The method to use when submitting the form, usually either "get" or "post".
18
      def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc)
19 20
        html_options = { "method" => "post" }.merge(options.stringify_keys)

21
        if html_options["multipart"]
22
          html_options["enctype"] = "multipart/form-data"
23
          html_options.delete("multipart")
24
        end
25

26 27 28 29 30 31 32 33 34 35 36
        html_options["action"] = url_for(url_for_options, *parameters_for_url)
        tag("form", html_options, true)
      end

      alias_method :start_form_tag, :form_tag

      # Outputs "</form>"
      def end_form_tag
        "</form>"
      end

37 38 39 40 41 42 43 44 45 46 47 48
      # Creates a dropdown selection box, or if the <tt>:multiple</tt> option is set to true, a multiple
      # choice selection box.
      #
      # Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or
      # associated records.
      #
      # <tt>option_tags</tt> is a string containing the option tags for the select box:
      #   # Outputs <select id="people" name="people"><option>David</option></select>
      #   select_tag "people", "<option>David</option>"
      #
      # Options:
      # * <tt>:multiple</tt> - If set to true the selection will allow multiple choices.
49
      def select_tag(name, option_tags = nil, options = {})
50
        content_tag("select", option_tags, { "name" => name, "id" => name }.update(options.stringify_keys))
51 52
      end

53 54 55 56 57 58 59 60
      # Creates a standard text field.
      #
      # Options:
      # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
      # * <tt>:size</tt> - The number of visible characters that will fit in the input.
      # * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
      # 
      # A hash of standard HTML options for the tag.
61
      def text_field_tag(name, value = nil, options = {})
62
        tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys))
63 64
      end

65 66 67
      # Creates a hidden field.
      #
      # Takes the same options as text_field_tag
68
      def hidden_field_tag(name, value = nil, options = {})
69
        text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
70 71
      end

72 73 74 75 76 77 78 79 80 81
      # Creates a file upload field.
      #
      # If you are using file uploads then you will also need to set the multipart option for the form:
      #   <%= form_tag { :action => "post" }, { :multipart => true } %>
      #     <label for="file">File to Upload</label> <%= file_field_tag "file" %>
      #     <%= submit_tag %>
      #   <%= end_form_tag %>
      #
      # The specified URL will then be passed a File object containing the selected file, or if the field 
      # was left blank, a StringIO object.
82
      def file_field_tag(name, options = {})
83
        text_field_tag(name, nil, options.update("type" => "file"))
84 85
      end

86 87 88
      # Creates a password field.
      #
      # Takes the same options as text_field_tag
89
      def password_field_tag(name = "password", value = nil, options = {})
90
        text_field_tag(name, value, options.update("type" => "password"))
91 92
      end

93 94 95 96 97
      # Creates a text input area.
      #
      # Options:
      # * <tt>:size</tt> - A string specifying the dimensions of the textarea.
      #     # Outputs <textarea name="body" id="body" cols="25" rows="10"></textarea>
98
      #     <%= text_area_tag "body", nil, :size => "25x10" %>
99
      def text_area_tag(name, content = nil, options = {})
100 101 102 103
        options = options.stringify_keys
        if options["size"]
          options["cols"], options["rows"] = options["size"].split("x")
          options.delete("size")
104
        end
105

106
        content_tag("textarea", content, { "name" => name, "id" => name }.update(options.stringify_keys))
107 108
      end

109
      # Creates a check box.
110
      def check_box_tag(name, value = "1", checked = false, options = {})
111
        html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
112 113 114 115
        html_options["checked"] = "checked" if checked
        tag("input", html_options)
      end

116
      # Creates a radio button.
117
      def radio_button_tag(name, value, checked = false, options = {})
118
        html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
119 120 121 122
        html_options["checked"] = "checked" if checked
        tag("input", html_options)
      end

123
      # Creates a submit button with the text <tt>value</tt> as the caption.
124
      def submit_tag(value = "Save changes", options = {})
125
        tag("input", { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys))
126
      end
127
      
128 129 130
      # Displays an image which when clicked will submit the form.
      #
      # <tt>source</tt> is passed to AssetTagHelper#image_path
131
      def image_submit_tag(source, options = {})
132
        tag("input", { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys))
133
      end
134 135 136
    end
  end
end