可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
The newest version of Devise doesn't have :confirmable enabled by default. I already added the respective columns to the User model but cannot find any code examples of how to enable :confirmable.
Where can I find a good example or what code do I need to enable it?
回答1:
to "enable" confirmable, you just need to add it to your model, e.g.:
class User
# ...
devise :confirmable , ....
# ...
end
after that, you'll have to create and run a migration which adds the required columns to your model:
# 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
see:
Adding confirmable module to an existing site using Devise
I'd recommend to check the source code to see how Confirmable works:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
You could also check the RailsCast on Devise:
http://railscasts.com/episodes/209-introducing-devise
Next it would be best to search for example applications on GitHub
回答2:
This question seems to be odd one ;-) If you written some migration alike:
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
and as you said little change in model (passing additional => :confirmable to devise) like so:
devise :database_authenticatable, :registerable, :confirmable
you can now generate some views (if you didn')
rails generate devise:views
You can go to app/views/devise/confirmations/new.html.erb and check how it looks like or change it. Furthermore you can inspect app/views/devise/confirmations/shared/_links.erb => there is line:
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
This condition checks if confirmable is turned on so... technically if everything went fine it should works OOTB. After creating new account - in log - you should see lines where confirmation mail is sent with appropriate link. It triggers:
Rendered devise/mailer/confirmation_instructions.html.erb
so you have got next place where you can customize it a bit
How to customize confirmation strategy? Please ask exact question what do you want to achieve. You can check devise gem path. In /lib/devise/models/confirmable.rb some comments could be helpful.
regards
回答3:
If you've already installed devise into your app, and want to add "confirmable" later, instead of running:
rails generate devise:views
as mentioned by Piotr, run
rails generate devise:views confirmable
to produce only the views needed for "confirmable". You'll see output like this:
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
You'll then be able to access these files directly in your project to style them like your application. You'll also be able to change the messaging in the emails Devise sends out through the generated mailer views.
Last, don't forget to add config.action_mailer.delivery_method and config.action_mailer.smtp_settings in your app/config/environments/{environment_name}.rb file. This is what my production.rb file looks like:
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 }
回答4:
Checkout devise wiki page. There is a full answer for your question.
回答5:
For DRY, you can also put mailer config in config/initializers/mail.rb like:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => '[redacted]',
:user_name => '[redacted]',
:password => '[redacted]',
:authentication => 'plain',
:enable_starttls_auto => true }
回答6:
After configuring the ActionMailer setting described above I had to make one last addition to the config/environments/development.rb file to fix an error page that would show up after registering a new user:
config.action_mailer.default_url_options = { :host => 'localhost' }
More details about this solution: Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]