8

サブドメインを処理するために、パスワードを忘れた場合の手順を調整する必要があります。リストされているように、メーラー、コントローラーをオーバーライドし、サブドメインヘルパーなどを追加するために、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 をメーラー ビューに取得するにはどうすればよいですか?

4

4 に答える 4

7

私は方法を見つけました。パッチを適用したり、サブドメインに連鎖させたりすることなく、より良い方法を見つけることができるかどうかを考えます。

基本的に、これを行うコントローラーをオーバーライドします。

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

また、ユーザー モデルでこのメソッドにモンキー パッチを適用する必要がありました。

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_mailDevise 内にあるパッチ メソッドをモンキーする必要がありました。

  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変数がメーラー テンプレートに表示されました。このソリューションには満足していませんが、これは出発点です。その上で改善策を考えます。

于 2013-04-14T01:08:55.587 に答える
1

これがあなたの質問をうまく解決すると思う更新された回答です - https://github.com/plataformatec/devise/wiki/How-To:-Send-emails-from-subdomains

私の場合、サブドメインは Accounts テーブルに保存されていました。これは@resource.subdomain、devise のメーラー ビューで使用できるようにするために行ったことです。

class User < ActiveRecord::Base  
  belongs_to :account

  # This allows me to do something like @user.subdomain
  def subdomain
    account.subdomain
  end
end

class Account < ActiveRecord::Base
  has_many :users
end
于 2013-05-30T04:16:21.987 に答える
1

デバイス 3.1 の場合、上記のユーザー モデルでのモンキー パッチは以下のようになります。これは、サブドメインが別のモデル (テナントなど) に格納されている場合で、アカウント、ユーザーなどの他のモデルとは関係ありません.. (current_tenant.subdomain のように検索)

def send_reset_password_instructions(subdomain)
  raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)

  self.reset_password_token   = enc
  self.reset_password_sent_at = Time.now.utc
  self.save(:validate => false)

  send_devise_notification(:reset_password_instructions, raw, {subdomain: subdomain})
  raw
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
于 2013-10-18T06:35:34.570 に答える