Missing host to link to! Please provide the :host parameter, set default_url_options[:host]
というエラーに対処した話。Controllerなど以外ではrequest
の情報がないので:host
を別途指定しないといけない。
やりたいこととしてはroot_url
をController以外で使いたかったから。具体的にはWebhookで投げるテキストにURLを含めたかった。
module Webhooks
class Send
include Rails.application.routes.url_helpers #root_urlを使うため。
def initialize
end
def call
url = root_url
end
end
end
普通はというかホスト名が決め打ちできるならAction Mailer の基礎 | Rails ガイドにあるように、config/application.rb
に書くのがいいようだが、今回はそうではない。
しかたがないのでApplicationController
でセットするようにした
class ApplicationController < ActionController::Base
before_filter :set_host
def set_host
Rails.application.routes.default_url_options[:host] = request.host_with_port
end
end