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
Creates a check box.
-
#end_form_tag ⇒ Object
Outputs “</form>”.
-
#file_field_tag(name, options = {}) ⇒ Object
Creates a file upload field.
-
#form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) ⇒ 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
Creates a hidden field.
-
#image_submit_tag(source, options = {}) ⇒ Object
Displays an image which when clicked will submit the form.
-
#password_field_tag(name = "password", value = nil, options = {}) ⇒ Object
Creates a password field.
-
#radio_button_tag(name, value, checked = false, options = {}) ⇒ Object
Creates a radio button.
-
#select_tag(name, option_tags = nil, options = {}) ⇒ Object
Creates a dropdown selection box, or if the
:multiple
option is set to true, a multiple choice selection box. -
#submit_tag(value = "Save changes", options = {}) ⇒ Object
Creates a submit button with the text
value
as the caption. -
#text_area_tag(name, content = nil, options = {}) ⇒ Object
Creates a text input area.
-
#text_field_tag(name, value = nil, options = {}) ⇒ Object
Creates a standard text field.
Instance Method Details
#check_box_tag(name, value = "1", checked = false, options = {}) ⇒ Object
Creates a check box.
142 143 144 145 146 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 142 def check_box_tag(name, value = "1", checked = false, = {}) = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(.stringify_keys) ["checked"] = "checked" if checked tag :input, end |
#end_form_tag ⇒ Object
Outputs “</form>”
63 64 65 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 63 def end_form_tag "</form>" end |
#file_field_tag(name, options = {}) ⇒ Object
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.
114 115 116 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 114 def file_field_tag(name, = {}) text_field_tag(name, nil, .update("type" => "file")) end |
#form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) ⇒ 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.
Examples:
-
form_tag('/posts') => <form action="/posts" method="post">
-
form_tag('/posts/1', :method => :put) => <form action="/posts/1" method="put">
-
form_tag('/upload', :multipart => true) => <form action="/upload" method="post" enctype="multipart/form-data">
ERb example:
<% form_tag '/posts' do -%>
<div><%= submit_tag 'Save' %></div>
<% end -%>
Will output:
<form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 33 def form_tag( = {}, = {}, *parameters_for_url, &block) = .stringify_keys ["enctype"] = "multipart/form-data" if .delete("multipart") ["action"] = url_for(, *parameters_for_url) method_tag = "" case method = .delete("method").to_s when /^get$/i # must be case-insentive, but can't use downcase as might be nil ["method"] = "get" when /^post$/i, "", nil ["method"] = "post" else ["method"] = "post" method_tag = content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method), :style => 'margin:0;padding:0') end if block_given? content = capture(&block) concat(tag(:form, , true) + method_tag, block.binding) concat(content, block.binding) concat("</form>", block.binding) else tag(:form, , true) + method_tag end end |
#hidden_field_tag(name, value = nil, options = {}) ⇒ Object
Creates a hidden field.
Takes the same options as text_field_tag
100 101 102 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 100 def hidden_field_tag(name, value = nil, = {}) text_field_tag(name, value, .stringify_keys.update("type" => "hidden")) end |
#image_submit_tag(source, options = {}) ⇒ Object
Displays an image which when clicked will submit the form.
source
is passed to AssetTagHelper#image_path
171 172 173 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 171 def image_submit_tag(source, = {}) tag :input, { "type" => "image", "src" => image_path(source) }.update(.stringify_keys) end |
#password_field_tag(name = "password", value = nil, options = {}) ⇒ Object
Creates a password field.
Takes the same options as text_field_tag
121 122 123 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 121 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
Creates a radio button.
149 150 151 152 153 154 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 149 def (name, value, checked = false, = {}) pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase = { "type" => "radio", "name" => name, "id" => "#{name}_#{pretty_tag_value}", "value" => value }.update(.stringify_keys) ["checked"] = "checked" if checked tag :input, end |
#select_tag(name, option_tags = nil, options = {}) ⇒ Object
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.
81 82 83 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 81 def select_tag(name, = nil, = {}) content_tag :select, , { "name" => name, "id" => name }.update(.stringify_keys) end |
#submit_tag(value = "Save changes", options = {}) ⇒ Object
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.
158 159 160 161 162 163 164 165 166 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 158 def submit_tag(value = "Save changes", = {}) .stringify_keys! if disable_with = .delete("disable_with") ["onclick"] = "this.disabled=true;this.value='#{disable_with}';this.form.submit();#{["onclick"]}" end tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(.stringify_keys) end |
#text_area_tag(name, content = nil, options = {}) ⇒ Object
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" %>
131 132 133 134 135 136 137 138 139 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 131 def text_area_tag(name, content = nil, = {}) .stringify_keys! if size = .delete("size") ["cols"], ["rows"] = size.split("x") end content_tag :textarea, content, { "name" => name, "id" => name }.update(.stringify_keys) end |
#text_field_tag(name, value = nil, options = {}) ⇒ Object
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.
93 94 95 |
# File 'lib/action_view/helpers/form_tag_helper.rb', line 93 def text_field_tag(name, value = nil, = {}) tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(.stringify_keys) end |