2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】ActionMailerで画像のURLを絶対パスで表示させる

Last updated at Posted at 2019-01-27

ActionMailerで送信したメールに絶対パスで画像を表示させたい中でハマった。

Mail内で以下のように表示させるために

index.html
 <img src="http://myfullappurl.dev/assets/myimage.png">

development.rbにて以下のようにメーラーを設定

development.rb
config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.default_url_options = { host: 'myfullappurl.dev' }

しかしmail内でのhtmlでは

index.html
 <img src="//myfullappurl.dev/assets/myimage.png">

とプロトコルが表示されない。railsコードに

asset_url_helper.rb
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}

有効なActionMailer URIを定義する正規表現があるようで

development.rb

config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = 'http://myfullappurl.dev'

にしなければならない。
https://codeday.me/bug/20180720/199491.html
https://stackoverflow.com/questions/29887668/how-to-use-image-url-in-rails-to-show-images-in-production

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?