如何在其工作流程的特定步骤上修改 Devise 的消息?

How to modify Devise's message on a specific step of its workflow?

注册 新用户后,我被重定向到 登录 页面,其中包含以下 flash[:alert] 消息:

"您需要先登录或注册 继续。"

我的用户模型使用 Devise 的 :confirmable 模块,因此如果用户在注册后看到修改后的消息,那就太好了:

"Thanks for signing up. We have sent you a confirmational email. Please check your email"

有没有办法实现?


关于 Devise 工作流程的说明:

目前用户不知道已向他发送确认电子邮件。只有当他尝试使用未经确认的电子邮件地址登录时,他才会看到 Devise 的失败消息:

"You have to confirm your email address before continuing."


解决方法如下: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)

我只执行了前 2 个步骤:

1) 创建注册控制器:

class RegistrationsController < Devise::RegistrationsController
  protected


  # TODO: will this  method be triggered one day?
  def after_sign_up_path_for(resource)
    # '/an/example/path'
    new_user_session_path
  end

  def after_inactive_sign_up_path_for(resource)
    new_user_session_path
  end
end

2) 将路线更改为:

devise_for :users, :controllers => {:registrations => 'registrations'}

您需要将其添加到 /config/locales/ 目录中的 devise.en.yml 文件中。在 devise > registrations 下,添加 signed_up_but_unconfirmed 并设置一个值

devise:
  registrations:
    signed_up_but_unconfirmed: "Thanks for signing up. We have sent you a confirmational email. Please check your email."

希望对您有所帮助!

首先,将 devise :confirmable 添加到您的 models/user.rb

devise :confirmable

然后,按照以下方式进行迁移:

rails g migration add_confirmable_to_devise

将生成 db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb。添加以下内容以进行迁移。

class AddConfirmableToDevise < ActiveRecord::Migration
  # Note: You can't use change, as User.update_all will fail in the down migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    # add_column :users, :unconfirmed_email, :string # Only if using reconfirmable
    add_index :users, :confirmation_token, unique: true
    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    User.all.update_all confirmed_at: Time.now
    # All existing user accounts should be able to log in after this.
    # Remind: Rails using SQLite as default. And SQLite has no such function :NOW.
    # Use :date('now') instead of :NOW when using SQLite.
    # => execute("UPDATE users SET confirmed_at = date('now')")
    # Or => User.all.update_all confirmed_at: Time.now
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
    # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
  end
end

执行迁移 rake db:migrate

如果不使用reconfirmable,更新config/initializers/devise.rb中的配置

config.reconfirmable = false

希望对您有所帮助。