Module: ActionView::Helpers::UrlHelper
Overview
Action View URL Helpers
Provides a set of methods for getting URLs that
depend on the routing subsystem (see ::ActionDispatch::Routing).
Constant Summary
TagHelper - Included
ARIA_PREFIXES, BOOLEAN_ATTRIBUTES, CLASS_PREFIXES, DATA_PREFIXES, PRE_CONTENT_STRINGS, TAG_TYPES
Class Method Summary
::ActiveSupport::Concern - Extended
| class_methods | Define class methods from given block. |
| included | Evaluate given block in context of base class, so that you can write class macros here. |
| prepended | Evaluate given block in context of base class, so that you can write class macros here. |
| append_features, prepend_features | |
Instance Method Summary
-
#mail_to(email_address, name = nil, html_options = {}, &block)
Creates a mailto link tag to the specified
email_address, which is also used as the name of the link unlessnameis specified. -
#phone_to(phone_number, name = nil, html_options = {}, &block)
Creates a TEL anchor link tag to the specified
phone_number. -
#sms_to(phone_number, name = nil, html_options = {}, &block)
Creates an SMS anchor link tag to the specified
phone_number. -
#url_for(options = nil)
Internal use only
Basic implementation of url_for to allow use helpers without routes existence.
- #_back_url private Internal use only
- #_filtered_referrer private Internal use only
TagHelper - Included
| #cdata_section | Returns a CDATA section with the given |
| #class_names | Alias for TagHelper#token_list. |
| #content_tag | Returns an HTML block tag of type |
| #escape_once | Returns an escaped version of |
| #tag | Modern syntax. |
| #token_list | Returns a string of tokens built from |
| #build_tag_values, #ensure_valid_html5_tag_name, #tag_builder | |
OutputSafetyHelper - Included
| #raw | This method outputs without escaping a string. |
| #safe_join | This method returns an HTML safe string similar to what |
| #to_sentence | Converts the array to a comma-separated sentence where the last element is joined by the connector word. |
CaptureHelper - Included
| #capture | The capture method extracts part of a template as a string object. |
| #content_for | Calling |
| #content_for? |
|
| #provide | The same as |
| #with_output_buffer | Use an alternate output buffer for the duration of the block. |
Instance Method Details
#_back_url (private)
# File 'actionview/lib/action_view/helpers/url_helper.rb', line 37
def _back_url # :nodoc: _filtered_referrer || "javascript:history.back()" end
#_filtered_referrer (private)
# File 'actionview/lib/action_view/helpers/url_helper.rb', line 42
def _filtered_referrer # :nodoc: if controller.respond_to?(:request) referrer = controller.request.env["HTTP_REFERER"] if referrer && URI(referrer).scheme != "javascript" referrer end end rescue URI::InvalidURIError end
#mail_to(email_address, name = nil, html_options = {}, &block)
Creates a mailto link tag to the specified email_address, which is
also used as the name of the link unless name is specified. Additional
HTML attributes for the link can be passed in html_options.
mail_to has several methods for customizing the email itself by
passing special keys to html_options.
Options
- :subject - Preset the subject line of the email.
- :body - Preset the body of the email.
- :cc - Carbon Copy additional recipients on the email.
- :bcc - Blind Carbon Copy additional recipients on the email.
- :reply_to - Preset the
Reply-Tofield of the email.
Obfuscation
Prior to Rails 4.0, mail_to provided options for encoding the address
in order to hinder email harvesters. To take advantage of these options,
install the actionview-encoded_mail_to gem.
Examples
mail_to "me@domain.com"
=> me@domain.com
mail_to "me@domain.com", "My email"
# => <a href="mailto:me@domain.com">My email</a>
mail_to "me@domain.com", cc: "ccaddress@domain.com",
subject: "This is an example email"
# => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">me@domain.com</a>
You can use a block as well if your link target is hard to fit into the name parameter. ::ERB example:
<%= mail_to "me@domain.com" do %>
<strong>Email me:</strong> <span>me@domain.com</span>
<% end %>
# => <a href="mailto:me@domain.com">
<strong>Email me:</strong> <span>me@domain.com</span>
</a>
# File 'actionview/lib/action_view/helpers/url_helper.rb', line 91
def mail_to(email_address, name = nil, = {}, &block) , name = name, nil if name.is_a?(Hash) = ( || {}).stringify_keys extras = %w{ cc bcc body subject reply_to }.map! { |item| option = .delete(item).presence || next "#{item.dasherize}=#{ERB::Util.url_encode(option)}" }.compact extras = extras.empty? ? "" : "?" + extras.join("&") encoded_email_address = ERB::Util.url_encode(email_address).gsub("%40", "@") ["href"] = "mailto:#{encoded_email_address}#{extras}" content_tag("a", name || email_address, , &block) end
#phone_to(phone_number, name = nil, html_options = {}, &block)
Creates a TEL anchor link tag to the specified phone_number. When the
link is clicked, the default app to make phone calls is opened and
prepopulated with the phone number.
If name is not specified, phone_number will be used as the name of
the link.
A country_code option is supported, which prepends a plus sign and the
given country code to the linked phone number. For example,
country_code: "01" will prepend +01 to the linked
phone number.
Additional HTML attributes for the link can be passed via html_options.
Options
- :country_code - Prepends the country code to the phone number
Examples
phone_to "1234567890"
=> 1234567890
phone_to "1234567890", "Phone me"
# => <a href="tel:1234567890">Phone me</a>
phone_to "1234567890", country_code: "01"
# => <a href="tel:+011234567890">1234567890</a>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
<%= phone_to "1234567890" do %>
<strong>Phone me:</strong>
<% end %>
# => <a href="tel:1234567890">
<strong>Phone me:</strong>
</a>
# File 'actionview/lib/action_view/helpers/url_helper.rb', line 198
def phone_to(phone_number, name = nil, = {}, &block) , name = name, nil if name.is_a?(Hash) = ( || {}).stringify_keys country_code = .delete("country_code").presence country_code = country_code.nil? ? "" : "+#{ERB::Util.url_encode(country_code)}" encoded_phone_number = ERB::Util.url_encode(phone_number) ["href"] = "tel:#{country_code}#{encoded_phone_number}" content_tag("a", name || phone_number, , &block) end
#sms_to(phone_number, name = nil, html_options = {}, &block)
Creates an SMS anchor link tag to the specified phone_number. When the
link is clicked, the default SMS messaging app is opened ready to send a
message to the linked phone number. If the body option is specified,
the contents of the message will be preset to body.
If name is not specified, phone_number will be used as the name of
the link.
A country_code option is supported, which prepends a plus sign and the
given country code to the linked phone number. For example,
country_code: "01" will prepend +01 to the linked
phone number.
Additional HTML attributes for the link can be passed via html_options.
Options
- :country_code - Prepend the country code to the phone number.
- :body - Preset the body of the message.
Examples
sms_to "5155555785"
=> 5155555785
sms_to "5155555785", country_code: "01"
# => <a href="sms:+015155555785;">5155555785</a>
sms_to "5155555785", "Text me"
# => <a href="sms:5155555785;">Text me</a>
sms_to "5155555785", body: "I have a question about your product."
# => <a href="sms:5155555785;?body=I%20have%20a%20question%20about%20your%20product">5155555785</a>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
<%= sms_to "5155555785" do %>
<strong>Text me:</strong>
<% end %>
# => <a href="sms:5155555785;">
<strong>Text me:</strong>
</a>
# File 'actionview/lib/action_view/helpers/url_helper.rb', line 147
def sms_to(phone_number, name = nil, = {}, &block) , name = name, nil if name.is_a?(Hash) = ( || {}).stringify_keys country_code = .delete("country_code").presence country_code = country_code ? "+#{ERB::Util.url_encode(country_code)}" : "" body = .delete("body").presence body = body ? "?&body=#{ERB::Util.url_encode(body)}" : "" encoded_phone_number = ERB::Util.url_encode(phone_number) ["href"] = "sms:#{country_code}#{encoded_phone_number};#{body}" content_tag("a", name || phone_number, , &block) end
#url_for(options = nil)
Basic implementation of url_for to allow use helpers without routes existence