Module: ActionView::Helpers::TagHelper

Includes:
ERB::Util
Included in:
InstanceTag
Defined in:
lib/action_view/helpers/tag_helper.rb

Overview

This is poor man’s Builder for the rare cases where you need to programmatically make tags but can’t use Builder.

Instance Method Summary collapse

Instance Method Details

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

Examples:

  • content_tag("p", "Hello world!") => <p>Hello world!</p>

  • content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => <div class="strong"><p>Hello world!</p></div>



21
22
23
# File 'lib/action_view/helpers/tag_helper.rb', line 21

def (name, content, options = {})
  "<#{name}#{tag_options(options)}>#{content}</#{name}>"
end

#end_form_tagObject

Outputs “</form>”



43
44
45
# File 'lib/action_view/helpers/tag_helper.rb', line 43

def end_form_tag
  "</form>"
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.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/action_view/helpers/tag_helper.rb', line 27

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

#tag(name, options = {}, open = false) ⇒ Object

Examples:

  • tag("br") => <br />

  • tag("input", { "type" => "text"}) => <input type="text" />



13
14
15
# File 'lib/action_view/helpers/tag_helper.rb', line 13

def tag(name, options = {}, open = false)
  "<#{name}#{tag_options(options)}" + (open ? ">" : " />")
end