如何启用:在Devise中确认?

Eva*_*nic 41 confirmation devise ruby-on-rails-3

最新版本的Devise没有:默认情况下启用确认.我已经将相应的列添加到用户模型,但找不到任何如何启用的代码示例:确认.

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

Til*_*ilo 72

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

class User
  # ...
  devise :confirmable , ....
  # ...
end
Run Code Online (Sandbox Code Playgroud)

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

# 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
Run Code Online (Sandbox Code Playgroud)

请参阅: 使用Devise将可确认模块添加到现有站点

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

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

您还可以检查设计上的RailsCast:

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

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

  • 您还需要添加以下行:add_column:users,:unconfirmed_email,:string (2认同)
  • 另请检查:[如何:添加:确认给用户](https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users) (2认同)

Pio*_*ior 19

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

    change_table(:users) do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token,   :unique => true
Run Code Online (Sandbox Code Playgroud)

正如你所说的那样,模型的变化很小(通过额外的=>:确认设计),如下所示:

    devise :database_authenticatable, :registerable, :confirmable
Run Code Online (Sandbox Code Playgroud)

你现在可以生成一些视图(如果你没有')

    rails generate devise:views
Run Code Online (Sandbox Code Playgroud)

您可以转到app/views/devise/confirmations/new.html.erb并查看其外观或更改方式.此外,您可以检查app/views/devise/confirmations/shared/_links.erb =>有行:

    <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
Run Code Online (Sandbox Code Playgroud)

这个条件检查确认是否打开所以...技术上如果一切正常,它应该工作OOTB.创建新帐户后 - 在日志中 - 您应该看到通过适当的链接发送确认邮件的行.它会触发:

     Rendered devise/mailer/confirmation_instructions.html.erb
Run Code Online (Sandbox Code Playgroud)

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

如何自定义确认策略?请问您确切的问题,您想要实现什么目标.你可以检查设计宝石路径.在/lib/devise/models/confirmable.rb中,一些评论可能会有所帮助.

问候

  • 应该注意的是,迁移的风格现在已经过时了.https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style (4认同)

小智 16

如果您已经在设计中安装了设备,并希望稍后添加"确认",而不是运行:

rails generate devise:views
Run Code Online (Sandbox Code Playgroud)

如Piotr所说,跑

rails generate devise:views confirmable
Run Code Online (Sandbox Code Playgroud)

仅生成"可确认"所需的视图.你会看到这样的输出:

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 
Run Code Online (Sandbox Code Playgroud)

然后,您就可以直接在项目中访问这些文件,以便像应用程序一样设置样式.您还可以更改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  }
Run Code Online (Sandbox Code Playgroud)

  • 呃,这有点过时了.这里创建的所有视图都无济于事.你真的需要:`创建应用程序/视图/确认/确认创建应用程序/视图/确认/确认/ new.html.erb创建应用程序/视图/确认/邮件/确认_instructions.html.erb` (2认同)

cin*_*zyk 8

Checkout 设计维基页面.你的问题有一个完整的答案.