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.

Instance Method Summary collapse

Instance Method Details

#check_box_tag(name, value = "1", checked = false, options = {}) ⇒ Object



63
64
65
66
67
# File 'lib/action_view/helpers/form_tag_helper.rb', line 63

def check_box_tag(name, value = "1", checked = false, options = {})
  html_options = {"type" => "checkbox", "name" => name, "id" => name, "value" => value}.update(options)
  html_options["checked"] = "checked" if checked
  tag("input", html_options)
end

#end_form_tagObject

Outputs ""



30
31
32
# File 'lib/action_view/helpers/form_tag_helper.rb', line 30

def end_form_tag
  "</form>"
end

#file_field_tag(name, options = {}) ⇒ Object



46
47
48
# File 'lib/action_view/helpers/form_tag_helper.rb', line 46

def file_field_tag(name, options = {})
  text_field_tag(name, nil, 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".


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/action_view/helpers/form_tag_helper.rb', line 14

def form_tag(url_for_options = {}, options = {}, *parameters_for_url)
  html_options = { "method" => "post" }.merge(options)
  
  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



42
43
44
# File 'lib/action_view/helpers/form_tag_helper.rb', line 42

def hidden_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.update("type" => "hidden"))
end

#password_field_tag(name = "password", value = nil, options = {}) ⇒ Object



50
51
52
# File 'lib/action_view/helpers/form_tag_helper.rb', line 50

def password_field_tag(name = "password", value = nil, options = {})
  text_field_tag(name, value, options.update("type" => "password"))
end

#radio_button_tag(name, value, checked = false, options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/action_view/helpers/form_tag_helper.rb', line 69

def radio_button_tag(name, value, checked = false, options = {})
  html_options = {"type" => "radio", "name" => name, "id" => name, "value" => value}.update(options)
  html_options["checked"] = "checked" if checked
  tag("input", html_options)
end

#select_tag(name, option_tags = nil, options = {}) ⇒ Object



34
35
36
# File 'lib/action_view/helpers/form_tag_helper.rb', line 34

def select_tag(name, option_tags = nil, options = {})
  ("select", option_tags, { "name" => name, "id" => name }.update(options))
end

#submit_tag(value = "Save changes", options = {}) ⇒ Object



75
76
77
# File 'lib/action_view/helpers/form_tag_helper.rb', line 75

def submit_tag(value = "Save changes", options = {})
  tag("input", {"type" => "submit", "name" => "submit", "value" => value}.update(options))
end

#text_area_tag(name, content = nil, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/action_view/helpers/form_tag_helper.rb', line 54

def text_area_tag(name, content = nil, options = {})
  if options[:size]
    options["cols"], options["rows"] = options[:size].split("x")
    options.delete(:size)
  end
  
  ("textarea", content, { "name" => name, "id" => name }.update(options))
end

#text_field_tag(name, value = nil, options = {}) ⇒ Object



38
39
40
# File 'lib/action_view/helpers/form_tag_helper.rb', line 38

def text_field_tag(name, value = nil, options = {})
  tag("input", {"type" => "text", "name" => name, "id" => name, "value" => value}.update(options))
end