require 'cgi' require 'action_view/helpers/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. # # NOTE: The html options disabled, readonly, and multiple can all be treated as booleans. So specifying :disabled => true # will give disabled="disabled". module FormTagHelper # Starts a form tag that points the action to an url configured with url_for_options just like # ActionController::Base#url_for. The method for the form defaults to POST. # # Examples: # * form_tag('/posts') =>
# * form_tag('/posts/1', :method => :put) => # * form_tag('/upload', :multipart => true) => # # ERb example: # <% form_tag '/posts' do -%> #
<%= submit_tag 'Save' %>
# <% end -%> # # Will output: #
# # Options: # * :multipart - If set to true, the enctype is set to "multipart/form-data". # * :method - The method to use when submitting the form, usually either "get" or "post". # If "put", "delete", or another verb is used, a hidden input with name _method # is added to simulate the verb over post. def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) html_options = html_options_for_form(url_for_options, options, *parameters_for_url) if block_given? form_tag_in_block(html_options, &block) else form_tag_html(html_options) end end alias_method :start_form_tag, :form_tag # Outputs "" def end_form_tag "" end deprecate :end_form_tag, :start_form_tag => :form_tag # Creates a dropdown selection box, or if the :multiple 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. # # option_tags is a string containing the option tags for the select box: # # Outputs # select_tag "people", "" # # Options: # * :multiple - If set to true the selection will allow multiple choices. def select_tag(name, option_tags = nil, options = {}) content_tag :select, option_tags, { "name" => name, "id" => name }.update(options.stringify_keys) end # Creates a standard text field. # # Options: # * :disabled - If set to true, the user will not be able to use this input. # * :size - The number of visible characters that will fit in the input. # * :maxlength - The maximum number of characters that the browser will allow the user to enter. # # A hash of standard HTML options for the tag. def text_field_tag(name, value = nil, options = {}) tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) end # Creates a hidden field. # # Takes the same options as text_field_tag def hidden_field_tag(name, value = nil, options = {}) text_field_tag(name, value, options.stringify_keys.update("type" => "hidden")) end # 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 } %> # <%= 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. def file_field_tag(name, options = {}) text_field_tag(name, nil, options.update("type" => "file")) end # Creates a password field. # # Takes the same options as text_field_tag def password_field_tag(name = "password", value = nil, options = {}) text_field_tag(name, value, options.update("type" => "password")) end # Creates a text input area. # # Options: # * :size - A string specifying the dimensions of the textarea. # # Outputs # <%= text_area_tag "body", nil, :size => "25x10" %> def text_area_tag(name, content = nil, options = {}) options.stringify_keys! if size = options.delete("size") options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split) end content_tag :textarea, content, { "name" => name, "id" => name }.update(options.stringify_keys) end # Creates a check box. def check_box_tag(name, value = "1", checked = false, options = {}) html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) html_options["checked"] = "checked" if checked tag :input, html_options end # Creates a radio button. def radio_button_tag(name, value, checked = false, options = {}) pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase html_options = { "type" => "radio", "name" => name, "id" => "#{name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys) html_options["checked"] = "checked" if checked tag :input, html_options end # Creates a submit button with the text value as the caption. If options contains a pair with the key of "disable_with", # then the value will be used to rename a disabled version of the submit button. def submit_tag(value = "Save changes", options = {}) options.stringify_keys! if disable_with = options.delete("disable_with") options["onclick"] = [ "this.setAttribute('originalValue', this.value)", "this.disabled=true", "this.value='#{disable_with}'", "#{options["onclick"]}", "return (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())", ].join(";") end tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) end # Displays an image which when clicked will submit the form. # # source is passed to AssetTagHelper#image_path def image_submit_tag(source, options = {}) tag :input, { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys) end private def html_options_for_form(url_for_options, options, *parameters_for_url) returning options.stringify_keys do |html_options| html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") html_options["action"] = url_for(url_for_options, *parameters_for_url) end end def extra_tags_for_form(html_options) case method = html_options.delete("method").to_s when /^get$/i # must be case-insentive, but can't use downcase as might be nil html_options["method"] = "get" '' when /^post$/i, "", nil html_options["method"] = "post" '' else html_options["method"] = "post" content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method), :style => 'margin:0;padding:0') end end def form_tag_html(html_options) extra_tags = extra_tags_for_form(html_options) tag(:form, html_options, true) + extra_tags end def form_tag_in_block(html_options, &block) content = capture(&block) concat(form_tag_html(html_options), block.binding) concat(content, block.binding) concat("", block.binding) end end end end