form_tag_helper.rb 3.9 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 9 10
    #
    # NOTE: The html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>disabled => :true</tt>
    # 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 17
      # 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".
      def form_tag(url_for_options = {}, options = {}, *parameters_for_url)
18 19
        html_options = { "method" => "post" }.merge(options.stringify_keys)

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

25 26 27 28 29 30 31 32 33 34 35
        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

36
      def select_tag(name, option_tags = nil, options = {})
37
        content_tag("select", option_tags, { "name" => name, "id" => name }.update(convert_options(options)))
38 39
      end

40
      def text_field_tag(name, value = nil, options = {})
41
        tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(convert_options(options)))
42 43
      end

44
      def hidden_field_tag(name, value = nil, options = {})
45
        text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
46 47
      end

48
      def file_field_tag(name, options = {})
49
        text_field_tag(name, nil, convert_options(options).update("type" => "file"))
50 51
      end

52
      def password_field_tag(name = "password", value = nil, options = {})
53
        text_field_tag(name, value, convert_options(options).update("type" => "password"))
54 55 56
      end

      def text_area_tag(name, content = nil, options = {})
57 58 59 60
        options = options.stringify_keys
        if options["size"]
          options["cols"], options["rows"] = options["size"].split("x")
          options.delete("size")
61
        end
62

63
        content_tag("textarea", content, { "name" => name, "id" => name }.update(convert_options(options)))
64 65 66
      end

      def check_box_tag(name, value = "1", checked = false, options = {})
67
        html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(convert_options(options))
68 69 70 71
        html_options["checked"] = "checked" if checked
        tag("input", html_options)
      end

72
      def radio_button_tag(name, value, checked = false, options = {})
73
        html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(convert_options(options))
74 75 76 77
        html_options["checked"] = "checked" if checked
        tag("input", html_options)
      end

78
      def submit_tag(value = "Save changes", options = {})
79
        tag("input", { "type" => "submit", "name" => "commit", "value" => value }.update(convert_options(options)))
80
      end
81
      
82 83 84 85
      def image_submit_tag(source, options = {})
        tag("input", { "type" => "image", "src" => image_path(source) }.update(convert_options(options)))
      end
      
86 87 88 89 90 91 92 93 94 95 96 97 98 99
      private
        def convert_options(options)
          options = options.stringify_keys
          %w( disabled readonly multiple ).each { |a| boolean_attribute(options, a) }
          options
        end
        
        def boolean_attribute(options, attribute)
          if options[attribute]
            options[attribute] = attribute
          else
            options.delete attribute
          end
        end
100 101 102
    end
  end
end