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".
- check_box_tag
- end_form_tag
- file_field_tag
- form_tag
- hidden_field_tag
- image_submit_tag
- password_field_tag
- radio_button_tag
- select_tag
- start_form_tag
- submit_tag
- text_area_tag
- text_field_tag
Creates a check box.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 110
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: html_options["checked"] = "checked" if checked
113: tag("input", html_options)
114: end
Outputs "</form>"
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 33
33: def end_form_tag
34: "</form>"
35: 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 82
82: def file_field_tag(name, options = {})
83: text_field_tag(name, nil, options.update("type" => "file"))
84: 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:
- :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".
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 18
18: def form_tag(url_for_options = {}, options = {}, *parameters_for_url)
19: html_options = { "method" => "post" }.merge(options.stringify_keys)
20:
21: if html_options["multipart"]
22: html_options["enctype"] = "multipart/form-data"
23: html_options.delete("multipart")
24: end
25:
26: html_options["action"] = url_for(url_for_options, *parameters_for_url)
27: tag("form", html_options, true)
28: end
Creates a hidden field.
Takes the same options as text_field_tag
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 68
68: def hidden_field_tag(name, value = nil, options = {})
69: text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
70: end
Displays an image which when clicked will submit the form.
source is passed to AssetTagHelper#image_path
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 131
131: def image_submit_tag(source, options = {})
132: tag("input", { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys))
133: end
Creates a password field.
Takes the same options as text_field_tag
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 89
89: def password_field_tag(name = "password", value = nil, options = {})
90: text_field_tag(name, value, options.update("type" => "password"))
91: end
Creates a radio button.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 117
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: html_options["checked"] = "checked" if checked
120: tag("input", html_options)
121: 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:
- :multiple - If set to true the selection will allow multiple choices.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 49
49: def select_tag(name, option_tags = nil, options = {})
50: content_tag("select", option_tags, { "name" => name, "id" => name }.update(options.stringify_keys))
51: end
Alias for form_tag
Creates a submit button with the text value as the caption.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 124
124: def submit_tag(value = "Save changes", options = {})
125: tag("input", { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys))
126: end
Creates a text input area.
Options:
- :size - A string specifying the dimensions of the textarea.
# Outputs <textarea name="body" id="body" cols="25" rows="10"></textarea> <%= text_area_tag "body", nil, :size => "25x10" %>
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 99
99: def text_area_tag(name, content = nil, options = {})
100: options = options.stringify_keys
101: if options["size"]
102: options["cols"], options["rows"] = options["size"].split("x")
103: options.delete("size")
104: end
105:
106: content_tag("textarea", content, { "name" => name, "id" => name }.update(options.stringify_keys))
107: 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/form_tag_helper.rb, line 61
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: end