サブドメインを処理するために、パスワードを忘れた場合の手順を調整する必要があります。リストされているように、メーラー、コントローラーをオーバーライドし、サブドメインヘルパーなどを追加するために、devise サイトの指示に従いました。
controllers/password_controller.rb
class PasswordsController < Devise::PasswordsController
def create
@subdomain = request.subdomain
super
end
end
ルート.rb
devise_for :users, controllers: { passwords: 'passwords' }
devise.rb
config.mailer = "UserMailer"
mailers/user_mailer.rb
class UserMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
def confirmation_instructions(record, opts={})
devise_mail(record, :confirmation_instructions, opts)
end
def reset_password_instructions(record, opts={})
devise_mail(record, :reset_password_instructions, opts)
end
def unlock_instructions(record, opts={})
devise_mail(record, :unlock_instructions, opts)
end
end
ビュー/user_mailer/reset_password_instructions.html.erb
<p>Hello <%= @resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token, :subdomain => @subdomain) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
helpers/subdomain_helper.rb
module SubdomainHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
host = Rails.application.config.action_mailer.default_url_options[:host]
[subdomain, host].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
アプリケーション.rb
config.to_prepare do
Devise::Mailer.class_eval do
helper :subdomain
end
end
これで、このコードはすべて機能しますが、メーラー ビューで @subdomain の値を取得できません。@subdomain をハードコードされた文字列に置き換えると、正しい URL が電子メールで渡されるので、コードがすべて正しいことがわかります。
コントローラーで定義されたインスタンス変数 @subdomain をメーラー ビューに取得するにはどうすればよいですか?