Rails将request.subdomain传递到自定义Devise邮件程序布局中 [英] Rails pass request.subdomain into a custom Devise mailer layout

查看:37
本文介绍了Rails将request.subdomain传递到自定义Devise邮件程序布局中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调整忘记密码的说明来处理子域。
我已经按照devise网站上的指示重写了邮件程序,控制器并添加了子域助手等。

I need to adapt the forgot password instructions to handle a subdomain. I have followed the instructions on the devise site to override the mailer, controller and add a subdomain helper etc. as listed:

controllers / password_controller.rb

controllers/password_controller.rb

class PasswordsController < Devise::PasswordsController
  def create
    @subdomain = request.subdomain
    super
  end
end

routes.rb

routes.rb

devise_for :users, controllers: { passwords: 'passwords' }

devise.rb

devise.rb

config.mailer = "UserMailer"

mailers / user_mailer。 rb

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

views / user_mailer / reset_password_instructions.html.erb

views/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

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

application.rb

application.rb

config.to_prepare do
  Devise::Mailer.class_eval do 
    helper :subdomain
  end
end

现在,此代码可以正常工作,但在邮件视图中无法获取@subdomain的值。如果我用硬编码的字符串替换@subdomain,那么正确的url将通过电子邮件传递,因此我知道代码都是正确的。

Now, this code is all working but it just can't get the value of @subdomain in the mailer view. If I replace @subdomain with a hard-coded string then the correct url is passed in the email so I know the code is all correct.

如何获取实例

推荐答案

我找到了一种方法。我想如果我能找到更好的方法而又不必胡闹修补程序并将其链接到子域。

I've found a way. I will think if I can find a better way without having to monkey patch stuff and having to chain it up the subdomain.

基本上,我这样做是重写控制器:

Basically, I override the controller doing this:

class PasswordsController < Devise::PasswordsController
  def create
    subdomain = request.subdomain
    @user = User.send_reset_password_instructions(params[:user].merge(subdomain: subdomain))

    if successfully_sent?(@user)
      respond_with({}, :location => after_sending_reset_password_instructions_path_for(:user))
    else
      respond_with(@user)
    end
  end
end

此外,我还不得不在用户模型上修补此方法:

Also, I had to monkey patch this methods on my user model:

def send_reset_password_instructions(subdomain)
  generate_reset_password_token! if should_generate_reset_token?
  send_devise_notification(:reset_password_instructions, subdomain: subdomain)
end

def self.send_reset_password_instructions(attributes={})
  recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
  recoverable.send_reset_password_instructions(attributes[:subdomain]) if recoverable.persisted?
  recoverable
end

最后,我不得不猴子修补 devise_mail 方法,该方法位于Devise中。

And finally, I had to monkey patch devise_mail methods, which lives inside Devise.

  Devise::Mailer.class_eval do
    def devise_mail(record, action, opts={})
      initialize_from_record(record)
      initialize_subdomain(opts.delete(:subdomain)) # do this only if the action is to recover a password.
      mail headers_for(action, opts)
    end

    def initialize_subdomain(subdomain)
      @subdomain = instance_variable_set("@subdomain", subdomain)
    end
  end

这样做, @subdomain 变量出现在邮件模板上。我对这种解决方案不满意,但这是一个起点。我会考虑对其进行任何改进。

Doing this, the @subdomain variable appeared on the mailer template. I'm not happy with this solution, but this is a starting point. I will think on any improvements on it.

这篇关于Rails将request.subdomain传递到自定义Devise邮件程序布局中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆