instance method sms_to

Ruby on Rails 6.1.7.10

Since v6.1.7.10

Available in: v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

sms_to(phone_number, name = nil, html_options = {}, &block)

Creates an SMS anchor link tag to the specified phone_number, 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.

When clicked, an SMS message is prepopulated with the passed phone number and optional body value.

sms_to has a body option for customizing the SMS message itself by passing special keys to html_options.

Options

  • :body - Preset the body of the message.

Examples

sms_to "5155555785"
# => <a href="sms:5155555785;">5155555785</a>

sms_to "5155555785", "Text me"
# => <a href="sms:5155555785;">Text me</a>

sms_to "5155555785", "Text me",
       body: "Hello Jim I have a question about your product."
# => <a href="sms:5155555785;?body=Hello%20Jim%20I%20have%20a%20question%20about%20your%20product">Text me</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>

Parameters

phone_number req
name opt = nil
html_options opt = {}
block block
Source
# File actionview/lib/action_view/helpers/url_helper.rb, line 618
      def sms_to(phone_number, name = nil, html_options = {}, &block)
        html_options, name = name, nil if block_given?
        html_options = (html_options || {}).stringify_keys

        extras = %w{ body }.map! { |item|
          option = html_options.delete(item).presence || next
          "#{item.dasherize}=#{ERB::Util.url_encode(option)}"
        }.compact
        extras = extras.empty? ? "" : "?&" + extras.join("&")

        encoded_phone_number = ERB::Util.url_encode(phone_number)
        html_options["href"] = "sms:#{encoded_phone_number};#{extras}"

        content_tag("a", name || phone_number, html_options, &block)
      end

Defined in actionview/lib/action_view/helpers/url_helper.rb line 618 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::UrlHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close