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

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, options = {})
  html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(convert_options(options))
  html_options["checked"] = "checked" if checked
  tag("input", html_options)
end

#end_form_tagObject

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, options = {})
  text_field_tag(name, nil, convert_options(options).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(url_for_options = {}, options = {}, *parameters_for_url)
  html_options = { "method" => "post" }.merge(options.stringify_keys)

  if html_options["multipart"]
    html_options["enctype"] = "multipart/form-data"
    html_options.delete("multipart")
  end

  html_options["action"] = url_for(url_for_options, *parameters_for_url)
  tag("form", html_options, 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, options = {})
  text_field_tag(name, value, options.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, options = {})
  text_field_tag(name, value, convert_options(options).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 radio_button_tag(name, value, checked = false, options = {})
  html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(convert_options(options))
  html_options["checked"] = "checked" if checked
  tag("input", html_options)
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, option_tags = nil, options = {})
  ("select", option_tags, { "name" => name, "id" => name }.update(convert_options(options)))
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", options = {})
  tag("input", { "type" => "submit", "name" => "submit", "value" => value }.update(convert_options(options)))
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, options = {})
  options = options.stringify_keys
  if options["size"]
    options["cols"], options["rows"] = options["size"].split("x")
    options.delete("size")
  end

  ("textarea", content, { "name" => name, "id" => name }.update(convert_options(options)))
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, options = {})
  tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(convert_options(options)))
end