0

在我的应用程序中,所有路由都限定为一个区域设置,该用户已选择如下:

scope ":locale", locale: /#{SpreeI18n::Config.supported_locales.join('|')}/ do
  devise_for :users, skip: :omniauth_callbacks
  get '/', to: 'homepage#index', :as => :homepage
end

当我想发送重置密码指令User.find(1).send_reset_password_instructions时,有一个问题:

Devise::Mailer#reset_password_instructions: processed outbound mail in 4249.9ms
ActionView::Template::Error: No route matches {:action=>"edit", :controller=>"devise/passwords", :reset_password_token=>"-zyuNkscVkwFn2awdm27"} missing required keys: [:locale]

如何传递语言环境以便发送重置令牌?

4

1 回答 1

1

让我们创建一个自定义控制器password

  1. 自定义routes.rb

    scope ":locale", locale: /#{SpreeI18n::Config.supported_locales.join('|')}/ do
      devise_for :users, skip: :omniauth_callbacks, controllers: { passwords: 'my_passwords' }
      get '/', to: 'homepage#index', :as => :homepage
    end
    
  2. my_passwords_controller.rb

    class MyPasswordsController < Devise::PasswordsController
      def create
        resource_params.merge!(locale: 'en') # use 'en' for eg
        super
      end
    end
    

然后send_reset_password_instructions功能将resource_params在发送电子邮件时采用您的自定义。

请参阅设计源代码以了解设计的详细功能!

于 2016-03-01T17:39:10.317 回答