Why is Link in Rails Mailer Email Showing Incorrectly?

14 Views Asked by At

'Rails 6.1', 'ruby 3.1.1'

This is the first time I've added a link in an html email sent via a rails mailer. The email works, it's sent (to me) and I can see it. But the hyperlink in it is showing oddly. Instead of showing as a clickable link, it shows with brackets [ ], which are not in the link_to tag text or path. In the line below, Living Muay Thai Students Site should be the clickable text in the link. Not sure why it's showing like this.

[www.johndcowan.com/living_muay_thai]Living Muay Thai Student Site

Code:

# mailer

def new_student_badge_email
  @student = params[:student]
  @badge = params[:badge]
  @admin = params[:admin]
  @url = 'www.johndcowan.com/living_muay_thai'
    
  mail(to: @student.email, subject: 'LMT: You Earned a New Badge')
end


# html email (in part)

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <p>
      Dear <%= @student.full_name %>
...
...
      You can login to the <%= link_to("Living Muay Thai Student Site", @url) %> and view all your badges.
...

When I get the email, the link_to link gets changed to this (as noted above):

[www.johndcowan.com/living_muay_thai]Living Muay Thai Student Site

The other rails tags, i.e. <%= @student.first_name %> work as they should.

Any ideas on what I'm doing wrong? How to fix this?

1

There are 1 best solutions below

0
John Cowan On

Found it~!

Add http:// to url.

In a StackOverflow thread (link below) I saw a note that suggested using http:// to preface the url. I do not use Thunderbird email, but thought I'd try this anyway, and it worked. The link shows correctly in a sent email.

Thread: Rails mailer and links not working under Thunderbird

In my mailer:

def new_student_badge_email
    @student = params[:student]
    @badge = params[:badge]
    @admin = params[:admin]
    @url = "http://www.johndcowan.com/living_muay_thai"
    
    mail(to: @student.email, subject: 'LMT You Earned a New Badge')   
  end

Odd how something so simple would change how a link is displayed in an email.