Read more

Rails: How to provide a public link in a mail

Emanuel
March 28, 2018Software engineer at makandra GmbH

Lets say we have a user with a contract whereas contract is a mounted carrierwave Show archive.org snapshot file.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Now we want to send the link to the contract in a mail. For this use case join the root_url with the public contract path in the mailer view:

Plain text email

URI.join(root_url, @user.contract.url)

HTML email

link_to('Show contract', URI.join(root_url, @user.contract.url).to_s)

Note: You need to follow http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views Show archive.org snapshot or Make ActionMailer use the current request host and protocol for URL generation to have the correct path and port. If you don't store the files local but remote (e.g. S3) the url will already include the full url.

Posted by Emanuel to makandra dev (2018-03-28 15:27)