LoginSignup
37

More than 5 years have passed since last update.

RailsでMissing host to link to!が出たときに。model内でURL組み立てる場合の設定

Posted at

前置き

あまりネットで検索しても困っている人を見つけられなかったので設計が変なきもするけれど、ちょっとだけはまったのでメモ

RUby2.4.1、Rails5.1で、ローカルのcentos(vagrant)でもHerokuでも発生。

対象エラー

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

ActionMailerのviewのなかで、modelのメソッドを読んでURLを設定するときに出た。

対策

routes.default_url_optionsに設定する必要がある。

/config/environments/development.rb
  host = 'samplehost'
  Rails.application.routes.default_url_options[:host] = host

production.rbにも同じように設定する必要があります。

関連

このエラーで検索すると環境設定ファイルに以下のように設定するとよいという記事がStack Overflowをはじめいろいろ出てくる。
ActionMailerのviewで直接URLを表示するときにはこれが必要。

/config/environments/development.rb
config.action_mailer.default_url_options = { host: "samplehost"}

その他

railsのconfig設定がどうなっているか、consoleなどで確認したいのだけれどどうしたらいいだろう?
rails cしてRails.applicationを閲覧すると巨大なオブジェクトはみえるけれど、action_mailer系は見えない。

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
37