module ActionView::Helpers::FormTagHelper
def check_box_tag(name, value = "1", checked = false, options = {})
check_box_tag 'eula', 'accepted', false, :disabled => true
# =>
check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
# =>
check_box_tag 'receive_email', 'yes', true
# =>
check_box_tag 'rock', 'rock music'
# =>
check_box_tag 'accept'
==== Examples
* Any other key creates standard HTML options for the tag.
* :disabled - If set to true, the user will not be able to use this input.
==== Options
Creates a check box form input tag.
def check_box_tag(name, value = "1", checked = false, options = {}) html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys) html_options["checked"] = "checked" if checked tag :input, html_options end
def extra_tags_for_form(html_options)
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" protect_against_forgery? ? content_tag(:div, token_tag, :style => 'margin:0;padding:0') : '' else html_options["method"] = "post" content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag, :style => 'margin:0;padding:0') end end
def field_set_tag(legend = nil, options = nil, &block)
<% end %>
<%= text_field_tag 'name' %>
<% field_set_tag nil, :class => 'format' do %>
# =>
<% end %>
<%= text_field_tag 'name' %>
<% field_set_tag 'Your details' do %>
# =>
<% end %>
<%= text_field_tag 'name' %>
<% field_set_tag do %>
=== Examples
options accept the same values as tag.
legend will become the fieldset's title (optional as per W3C).
Creates a field set for grouping HTML form elements.
def field_set_tag(legend = nil, options = nil, &block) content = capture(&block) concat(tag(:fieldset, options, true)) concat(content_tag(:legend, legend)) unless legend.blank? concat(content) concat("</fieldset>") end
def file_field_tag(name, options = {})
file_field_tag 'file', :accept => 'text/html', :class => 'upload', :value => 'index.html'
# =>
file_field_tag 'user_pic', :accept => 'image/png,image/gif,image/jpeg'
# =>
file_field_tag 'resume', :value => '~/resume.doc'
# =>
file_field_tag 'picture', :disabled => true
# =>
file_field_tag 'avatar', :class => 'profile-input'
# =>
file_field_tag 'attachment'
==== Examples
* :disabled - If set to true, the user will not be able to use this input.
* Creates standard HTML attributes for the tag.
==== Options
was left blank, a StringIO object.
The specified URL will then be passed a File object containing the selected file, or if the field
<% end %>
<%= submit_tag %>
<%= file_field_tag "file" %>
<% form_tag '/upload', :multipart => true do %>
to set the multipart option for the form tag:
Creates a file upload field. If you are using file uploads then you will also need
def file_field_tag(name, options = {}) text_field_tag(name, nil, options.update("type" => "file")) end
def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)
<% end -%>
<%= submit_tag 'Save' %>
<% form_tag '/posts' do -%>
# =>
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
def form_tag_html(html_options)
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)
def form_tag_in_block(html_options, &block) content = capture(&block) concat(form_tag_html(html_options)) concat(content) concat("</form>") end
def hidden_field_tag(name, value = nil, options = {})
# => hidden_field_tag 'collected_input', '', :onchange => "alert('Input collected!')"
# =>
hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
# =>
hidden_field_tag 'tags_list'
==== Examples
* Creates standard HTML attributes for the tag.
==== Options
data that should be hidden from the user.
Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or
def hidden_field_tag(name, value = nil, options = {}) text_field_tag(name, value, options.stringify_keys.update("type" => "hidden")) end
def html_options_for_form(url_for_options, options, *parameters_for_url)
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 image_submit_tag(source, options = {})
image_submit_tag("agree.png", :disabled => true, :class => "agree-disagree-button")
# =>
image_submit_tag("search.png", :class => 'search-button')
# =>
image_submit_tag("purchase.png", :disabled => true)
# =>
image_submit_tag("login.png")
==== Examples
* Any other key creates standard HTML options for the tag.
* :disabled - If set to true, the user will not be able to use this input.
processed normally, otherwise no action is taken.
prompt with the question specified. If the user accepts, the form is
* :confirm => 'question?' - This will add a JavaScript confirm
==== Options
source is passed to AssetTagHelper#image_path
Displays an image which when clicked will submit the form.
def image_submit_tag(source, options = {}) options.stringify_keys! if confirm = options.delete("confirm") options["onclick"] ||= '' options["onclick"] += "return #{confirm_javascript_function(confirm)};" end tag :input, { "type" => "image", "src" => path_to_image(source) }.update(options.stringify_keys) end
def label_tag(name, text = nil, options = {})
label_tag 'name', nil, :class => 'small_label'
# =>
label_tag 'name', 'Your name'
# =>
label_tag 'name'
==== Examples
* Creates standard HTML attributes for the tag.
==== Options
Creates a label field
def label_tag(name, text = nil, options = {}) content_tag :label, text || name.to_s.humanize, { "for" => sanitize_to_id(name) }.update(options.stringify_keys) end
def password_field_tag(name = "password", value = nil, options = {})
password_field_tag 'pin', '1234', :maxlength => 4, :size => 6, :class => "pin-input"
# =>
password_field_tag 'confirm_pass', nil, :disabled => true
# =>
password_field_tag 'key', nil, :maxlength => 16
# =>
password_field_tag 'token', '', :size => 15
# =>
password_field_tag 'masked', nil, :class => 'masked_input_field'
# =>
password_field_tag 'secret', 'Your secret here'
# =>
password_field_tag 'pass'
==== Examples
* Any other key creates standard HTML attributes for the tag.
* :maxlength - The maximum number of characters that the browser will allow the user to enter.
* :size - The number of visible characters that will fit in the input.
* :disabled - If set to true, the user will not be able to use this input.
==== Options
Creates a password field, a masked text field that will hide the users input behind a mask character.
def password_field_tag(name = "password", value = nil, options = {}) text_field_tag(name, value, options.update("type" => "password")) end
def radio_button_tag(name, value, checked = false, options = {})
radio_button_tag 'color', "green", true, :class => "color_input"
# =>
radio_button_tag 'time_slot', "3:00 p.m.", false, :disabled => true
# =>
radio_button_tag 'receive_updates', 'no', true
# =>
radio_button_tag 'gender', 'male'
==== Examples
* Any other key creates standard HTML options for the tag.
* :disabled - If set to true, the user will not be able to use this input.
==== Options
select from a group of options.
Creates a radio button; use groups of radio buttons named the same to allow users to
def radio_button_tag(name, value, checked = false, options = {}) pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase pretty_name = name.to_s.gsub(/\[/, "_").gsub(/\]/, "") html_options = { "type" => "radio", "name" => name, "id" => "#{pretty_name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys) html_options["checked"] = "checked" if checked tag :input, html_options end
def sanitize_to_id(name)
def sanitize_to_id(name) name.to_s.gsub(']','').gsub(/[^-a-zA-Z0-9:.]/, "_") end
def select_tag(name, option_tags = nil, options = {})
# =>
# =>
# =>
# =>
# =>
def select_tag(name, option_tags = nil, options = {}) html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys) end
def submit_tag(value = "Save changes", options = {})
# => submit_tag "Edit", :disable_with => "Editing...", :class => "edit-button"
# =>
submit_tag nil, :class => "form_submit"
# type="submit" value="Complete sale" />
# => submit_tag "Complete sale", :disable_with => "Please wait..."
# =>
submit_tag "Save edits", :disabled => true
# =>
submit_tag "Edit this article"
# =>
submit_tag
==== Examples
* Any other key creates standard HTML options for the tag.
of the submit button when the form is submitted.
* :disable_with - Value of this parameter will be used as the value for a disabled version
* :disabled - If true, the user will not be able to use this input.
processed normally, otherwise no action is taken.
prompt with the question specified. If the user accepts, the form is
* :confirm => 'question?' - This will add a JavaScript confirm
==== Options
Creates a submit button with the text value as the caption.
def submit_tag(value = "Save changes", options = {}) options.stringify_keys! if disable_with = options.delete("disable_with") disable_with = "this.value='#{disable_with}'" disable_with << ";#{options.delete('onclick')}" if options['onclick'] options["onclick"] = "if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }" options["onclick"] << "else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }" options["onclick"] << "this.setAttribute('originalValue', this.value);this.disabled = true;#{disable_with};" options["onclick"] << "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());" options["onclick"] << "if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" end if confirm = options.delete("confirm") options["onclick"] ||= 'return true;' options["onclick"] = "if (!#{confirm_javascript_function(confirm)}) return false; #{options['onclick']}" end tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) end
def text_area_tag(name, content = nil, options = {})
text_area_tag 'comment', nil, :class => 'comment_input'
# =>
text_area_tag 'description', "Description goes here.", :disabled => true
# =>
text_area_tag 'body', nil, :size => "25x10"
# =>
text_area_tag 'body', nil, :rows => 10, :cols => 25
# =>
text_area_tag 'bio', @user.bio
# =>
text_area_tag 'post'
==== Examples
* Any other key creates standard HTML attributes for the tag.
* :disabled - If set to true, the user will not be able to use this input.
* :cols - Specify the number of columns in the textarea
* :rows - Specify the number of rows in the textarea
* :size - A string specifying the dimensions (columns by rows) of the textarea (e.g., "25x10").
==== Options
Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
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
def text_field_tag(name, value = nil, options = {})
text_field_tag 'ip', '0.0.0.0', :maxlength => 15, :size => 20, :class => "ip-input"
# =>
text_field_tag 'payment_amount', '$0.00', :disabled => true
# =>
text_field_tag 'zip', nil, :maxlength => 5
# =>
text_field_tag 'address', '', :size => 75
# =>
text_field_tag 'request', nil, :class => 'special_input'
# =>
text_field_tag 'query', 'Enter your search query here'
# =>
text_field_tag 'name'
==== Examples
* Any other key creates standard HTML attributes for the tag.
* :maxlength - The maximum number of characters that the browser will allow the user to enter.
* :size - The number of visible characters that will fit in the input.
* :disabled - If set to true, the user will not be able to use this input.
==== Options
or a search query.
Creates a standard text field; use these text fields to input smaller chunks of text like a username
def text_field_tag(name, value = nil, options = {}) tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys) end
def token_tag
def token_tag unless protect_against_forgery? '' else tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) end end