Rails 6 sms link helper


Rails 6.1 now supports rendering sms links. We can use the the recently added sms_to rendering SMS anchor links.

This SMS anchor link when clicked pops open user’s SMS client targetting the specified phone number.

New message window is poped open on IPhone. New message window is poped open on IPhone.

Using sms_to

Here are few ways you can use the sms_to helper in your Rails views:

We can just pass the phone number. In this case the phone number is also used as the name of the link.

ERB code:

<%= sms_to "2025550181" %>

Rendered HTML:

<a href="sms:2025550181;">
  2025550181
</a>

We can explicitly specify the name of the link.

ERB code:

<%= sms_to "2025550181", "Text me!" %>

Rendered HTML:

<a href="sms:2025550181;">
  Text me!
</a>

We can specify the content of the SMS body.

ERB code:

<%= sms_to "2025550181", "Text me!", body: "Hi, I have few question:" %>

Rendered HTML:

<a href="sms:2025550181;?&amp;body=Hi%2C%20I%20have%20few%20question%3A">
  Text me!
</a>

We can also pass a block to the sms_to helper if we want more control over the content of the rendered SMS anchor tag:

ERB code:

<%= sms_to "2025550181" do %>
  <strong>Text me!</strong>
<% end %>

Rendered HTML:

<a href="sms:2025550181;">
  <strong>Text me!</strong>
</a>