Module: ActionView::Helpers::FormTagHelper
- Defined in:
- lib/action_view/helpers/form_tag_helper.rb
Overview
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"
.
Instance Method Summary collapse
- #check_box_tag(name, value = "1", checked = false, options = {}) ⇒ Object
-
#end_form_tag ⇒ Object
Outputs “</form>”.
- #file_field_tag(name, options = {}) ⇒ Object
-
#form_tag(url_for_options = {}, options = {}, *parameters_for_url) ⇒ Object
(also: #start_form_tag)
Starts a form tag that points the action to an url configured with
url_for_options
just like ActionController::Base#url_for. - #hidden_field_tag(name, value = nil, options = {}) ⇒ Object
- #password_field_tag(name = "password", value = nil, options = {}) ⇒ Object
- #radio_button_tag(name, value, checked = false, options = {}) ⇒ Object
- #select_tag(name, option_tags = nil, options = {}) ⇒ Object
- #submit_tag(value = "Save changes", options = {}) ⇒ Object
- #text_area_tag(name, content = nil, options = {}) ⇒ Object
- #text_field_tag(name, value = nil, options = {}) ⇒ Object
Instance Method Details
#check_box_tag(name, value = "1", checked = false, options = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 66 def check_box_tag(name, value = "1", checked = false, = {}) = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(()) ["checked"] = "checked" if checked tag("input", ) end |
#end_form_tag ⇒ Object
Outputs “</form>”
32 33 34 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 32 def end_form_tag "</form>" end |
#file_field_tag(name, options = {}) ⇒ Object
48 49 50 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 48 def file_field_tag(name, = {}) text_field_tag(name, nil, ().update("type" => "file")) end |
#form_tag(url_for_options = {}, options = {}, *parameters_for_url) ⇒ Object Also known as: start_form_tag
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”.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 17 def form_tag( = {}, = {}, *parameters_for_url) = { "method" => "post" }.merge(.stringify_keys) if ["multipart"] ["enctype"] = "multipart/form-data" .delete("multipart") end ["action"] = url_for(, *parameters_for_url) tag("form", , true) end |
#hidden_field_tag(name, value = nil, options = {}) ⇒ Object
44 45 46 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 44 def hidden_field_tag(name, value = nil, = {}) text_field_tag(name, value, .stringify_keys.update("type" => "hidden")) end |
#password_field_tag(name = "password", value = nil, options = {}) ⇒ Object
52 53 54 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 52 def password_field_tag(name = "password", value = nil, = {}) text_field_tag(name, value, ().update("type" => "password")) end |
#radio_button_tag(name, value, checked = false, options = {}) ⇒ Object
72 73 74 75 76 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 72 def (name, value, checked = false, = {}) = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(()) ["checked"] = "checked" if checked tag("input", ) end |
#select_tag(name, option_tags = nil, options = {}) ⇒ Object
36 37 38 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 36 def select_tag(name, = nil, = {}) content_tag("select", , { "name" => name, "id" => name }.update(())) end |
#submit_tag(value = "Save changes", options = {}) ⇒ Object
78 79 80 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 78 def submit_tag(value = "Save changes", = {}) tag("input", { "type" => "submit", "name" => "submit", "value" => value }.update(())) end |
#text_area_tag(name, content = nil, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 56 def text_area_tag(name, content = nil, = {}) = .stringify_keys if ["size"] ["cols"], ["rows"] = ["size"].split("x") .delete("size") end content_tag("textarea", content, { "name" => name, "id" => name }.update(())) end |
#text_field_tag(name, value = nil, options = {}) ⇒ Object
40 41 42 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 40 def text_field_tag(name, value = nil, = {}) tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(())) end |