我需要调整忘记密码的说明来处理子域。我已按照设计站点上的说明覆盖邮件程序、控制器并添加子域帮助程序等,如下所示:
控制器/password_controller.rb
class PasswordsController < Devise::PasswordsController
def create
@subdomain = request.subdomain
super
end
end
路线.rb
devise_for :users, controllers: { passwords: 'passwords' }
设计.rb
config.mailer = "UserMailer"
邮件程序/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>
助手/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 放入邮件程序视图中?