Module | ActionView::Helpers::FormTagHelper |
In: |
vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb
|
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".
Creates a check box.
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 120 120: def check_box_tag(name, value = "1", checked = false, options = {}) 121: html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) 122: html_options["checked"] = "checked" if checked 123: tag :input, html_options 124: end
Outputs "</form>"
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 43 43: def end_form_tag 44: "</form>" 45: 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 } %> <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.
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 92 92: def file_field_tag(name, options = {}) 93: text_field_tag(name, nil, options.update("type" => "file")) 94: end
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.
Options:
If "put", "delete", or another verb is used, a hidden input with name _method is added to simulate the verb over post.
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 20 20: def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) 21: html_options = options.stringify_keys 22: html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") 23: html_options["action"] = url_for(url_for_options, *parameters_for_url) 24: 25: method_tag = "" 26: 27: case method = html_options.delete("method") 28: when /^get$/i # must be case-insentive, but can't use downcase as might be nil 29: html_options["method"] = "get" 30: when /^post$/i, nil 31: html_options["method"] = "post" 32: else 33: html_options["method"] = "post" 34: method_tag = tag(:input, :type => "hidden", :name => "_method", :value => method) 35: end 36: 37: tag(:form, html_options, true) + method_tag 38: end
Creates a hidden field.
Takes the same options as text_field_tag
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 78 78: def hidden_field_tag(name, value = nil, options = {}) 79: text_field_tag(name, value, options.stringify_keys.update("type" => "hidden")) 80: end
Displays an image which when clicked will submit the form.
source is passed to AssetTagHelper#image_path
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 149 149: def image_submit_tag(source, options = {}) 150: tag :input, { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys) 151: end
Creates a password field.
Takes the same options as text_field_tag
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 99 99: def password_field_tag(name = "password", value = nil, options = {}) 100: text_field_tag(name, value, options.update("type" => "password")) 101: end
Creates a radio button.
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 127 127: def radio_button_tag(name, value, checked = false, options = {}) 128: pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase 129: html_options = { "type" => "radio", "name" => name, "id" => "#{name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys) 130: html_options["checked"] = "checked" if checked 131: tag :input, html_options 132: end
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 id="people" name="people"><option>David</option></select> select_tag "people", "<option>David</option>"
Options:
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 59 59: def select_tag(name, option_tags = nil, options = {}) 60: content_tag :select, option_tags, { "name" => name, "id" => name }.update(options.stringify_keys) 61: 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.
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 136 136: def submit_tag(value = "Save changes", options = {}) 137: options.stringify_keys! 138: 139: if disable_with = options.delete("disable_with") 140: options["onclick"] = "this.disabled=true;this.value='#{disable_with}';this.form.submit();#{options["onclick"]}" 141: end 142: 143: tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) 144: end
Creates a text input area.
Options:
# Outputs <textarea name="body" id="body" cols="25" rows="10"></textarea> <%= text_area_tag "body", nil, :size => "25x10" %>
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 109 109: def text_area_tag(name, content = nil, options = {}) 110: options.stringify_keys! 111: 112: if size = options.delete("size") 113: options["cols"], options["rows"] = size.split("x") 114: end 115: 116: content_tag :textarea, content, { "name" => name, "id" => name }.update(options.stringify_keys) 117: end
Creates a standard text field.
Options:
A hash of standard HTML options for the tag.
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 71 71: def text_field_tag(name, value = nil, options = {}) 72: tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) 73: end