【问题标题】:How do I enable :confirmable in Devise?如何在设计中启用 :confirmable?
【发布时间】:2011-06-14 13:55:07
【问题描述】:

Devise 的最新版本默认没有启用 :confirmable。我已经将相应的列添加到 User 模型中,但找不到任何关于如何启用 :confirmable 的代码示例。

我在哪里可以找到一个很好的例子或者我需要什么代码来启用它?

【问题讨论】:

    标签: ruby-on-rails-3 devise confirmation


    【解决方案1】:

    配置上述 ActionMailer 设置后,我必须在 config/environments/development.rb 文件中添加最后一个内容,以修复注册新用户后会显示的错误页面:

    config.action_mailer.default_url_options = { :host => 'localhost' }

    有关此解决方案的更多详细信息:Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]

    【讨论】:

      【解决方案2】:

      结帐devise wiki page。你的问题有一个完整的答案。

      【讨论】:

        【解决方案3】:

        要“启用”可确认,您只需将其添加到您的模型中,例如:

        class User
          # ...
          devise :confirmable , ....
          # ...
        end
        

        之后,您必须创建并运行迁移,将所需的列添加到您的模型中:

        # rails g migration add_confirmable_to_devise
        class AddConfirmableToDevise < ActiveRecord::Migration
          def self.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
        
            add_index  :users, :confirmation_token, :unique => true
          end
          def self.down
            remove_index  :users, :confirmation_token
        
            remove_column :users, :unconfirmed_email
            remove_column :users, :confirmation_sent_at
            remove_column :users, :confirmed_at
            remove_column :users, :confirmation_token
          end
        end
        

        见: Adding confirmable module to an existing site using Devise

        我建议检查源代码以了解 Confirmable 的工作原理:

        https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb

        您也可以在 Devise 上查看 RailsCast:

        http://railscasts.com/episodes/209-introducing-devise

        接下来最好在 GitHub 上搜索示例应用程序

        【讨论】:

        • 这应该被标记为答案,以及添加来自“stackoverflow.com/a/7577878/602588”的位(答案如下)。
        • 您还需要添加以下行:add_column :users, :unconfirmed_email, :string
        • 这是 Devise 最近的变化吗?
        • 谢谢!帮助我在大约 2 分钟内在我的应用中启用它。一个提示:如果您在已经注册用户之后启用它,那么确认可能对他们不起作用(它不适合我)。我重新创建了用户,确认了帐户,一切正常。
        【解决方案4】:

        对于 DRY,您还可以将邮件配置放在 config/initializers/mail.rb 中,例如:

        ActionMailer::Base.smtp_settings = {
            :address              => "smtp.gmail.com",
            :port                 => 587,
            :domain               => '[redacted]',
            :user_name            => '[redacted]',
            :password             => '[redacted]',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }
        

        【讨论】:

          【解决方案5】:

          如果您已经将设计安装到您的应用中,并且想稍后添加“可确认”,而不是运行:

          rails generate devise:views
          

          如 Piotr 所说,运行

          rails generate devise:views confirmable
          

          只生成“可确认”所需的视图。你会看到这样的输出:

          rails generate devise:views confirmable
              invoke  Devise::Generators::SharedViewsGenerator
              create    app/views/confirmable/mailer
              create    app/views/confirmable/mailer/confirmation_instructions.html.erb
              create    app/views/confirmable/mailer/reset_password_instructions.html.erb
              create    app/views/confirmable/mailer/unlock_instructions.html.erb
              create    app/views/confirmable/shared
              create    app/views/confirmable/shared/_links.erb
              invoke  form_for
              create    app/views/confirmable/confirmations
              create    app/views/confirmable/confirmations/new.html.erb
              create    app/views/confirmable/passwords
              create    app/views/confirmable/passwords/edit.html.erb
              create    app/views/confirmable/passwords/new.html.erb
              create    app/views/confirmable/registrations
              create    app/views/confirmable/registrations/edit.html.erb
              create    app/views/confirmable/registrations/new.html.erb
              create    app/views/confirmable/sessions
              create    app/views/confirmable/sessions/new.html.erb
              create    app/views/confirmable/unlocks
              create    app/views/confirmable/unlocks/new.html.erb 
          

          然后您就可以直接在您的项目中访问这些文件,以像您的应用程序一样设置它们的样式。您还可以更改 Devise 通过生成的邮件程序视图发送的电子邮件中的消息。

          最后,不要忘记在 app/config/environments/{environment_name}.rb 文件中添加 config.action_mailer.delivery_method 和 config.action_mailer.smtp_settings。这是我的 production.rb 文件的样子:

            config.action_mailer.delivery_method = :smtp
            config.action_mailer.smtp_settings = {
              :address              => "smtp.gmail.com",
              :port                 => 587,
              :domain               => '[redacted]',
              :user_name            => '[redacted]',
              :password             => '[redacted]',
              :authentication       => 'plain',
              :enable_starttls_auto => true  }
          

          【讨论】:

          • 不需要按照 Tilo 的建议进行迁移?
          • 呃,这有点不对劲。此处创建的所有视图都无济于事。你只需要:create app/views/confirmable/confirmations create app/views/confirmable/confirmations/new.html.erb create app/views/confirmable/mailer/confirmation_instructions.html.erb
          • 嗨,:domain 的正确设置是什么?它是我的 heroku 应用程序的域还是?
          【解决方案6】:

          这个问题似乎很奇怪;-) 如果你写了一些类似的迁移:

              change_table(:users) do |t|
                t.confirmable
              end
              add_index :users, :confirmation_token,   :unique => true
          

          正如你所说,模型几乎没有变化(通过额外的 => :confirmable 来设计),如下所示:

              devise :database_authenticatable, :registerable, :confirmable
          

          您现在可以生成一些视图(如果没有的话)

              rails generate devise:views
          

          您可以转到 app/views/devise/confirmations/new.html.erb 并检查它的外观或更改它。此外,您可以检查 app/views/devise/confirmations/shared/_links.erb => 有一行:

              <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
          

          此条件检查可确认是否已打开,所以...从技术上讲,如果一切正常,它应该可以正常工作 OOTB。创建新帐户后 - 登录 - 您应该会看到通过适当链接发送确认邮件的行。它触发:

               Rendered devise/mailer/confirmation_instructions.html.erb
          

          所以你有下一个可以自定义的地方

          如何自定义确认策略?请提出确切的问题,您想实现什么。您可以检查设计 gem 路径。在 /lib/devise/models/confirmable.rb 中,一些 cmets 可能会有所帮助。

          问候

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-09
          相关资源
          最近更新 更多