Devise and confirmation emails
You can override devise mailer to meet your requirements.
First of all create DeviseMailer
# app/mailers/devise/mailer.eb
if defined?(ActionMailer)
class Devise::Mailer < Devise.parent_mailer.constantize
include Devise::Mailers::Helpers
def confirmation_instructions(record, token, opts = {})
@token = token
if record.pending_reconfirmation?
devise_mail(record, :reconfirmation_instructions, opts)
else
devise_mail(record, :confirmation_instructions, opts)
end
end
def reset_password_instructions(record, token, opts = {})
@token = token
devise_mail(record, :reset_password_instructions, opts)
end
def unlock_instructions(record, token, opts = {})
@token = token
devise_mail(record, :unlock_instructions, opts)
end
end
end
Then just create needed views in app/views/devise/mailer/
for each of methods:
- reconfirmation_instructions, will be called when user changes they email
- confirmation_instructions, will be called when user confirms they email
- unlock_instructions, when account is locked
- reset_instructions, on password reset
Actually you can create any template you'd like.
Hope that will help.
How to prevent automatic signin after confirmation and setting password using devise
Change your config/initializers/devise.rb
you'll see...
# When set to false, does not sign a user in automatically after their password is
# reset. Defaults to true, so a user is signed in automatically after a reset.
# config.sign_in_after_reset_password = true
Uncomment and change the last line to
config.sign_in_after_reset_password = false
How to change Devise: password reset instruction email's subject
you can change it in devise.en.yml file in intilizer directory
And set your own subject for any mailer
mailer:
confirmation_instructions:
subject: 'Confirmation instructions'
reset_password_instructions:
subject: 'Reset password instructions'
unlock_instructions:
subject: 'Unlock Instructions'
Devise: Disable password confirmation during sign-up
To disable password confirmation you can simply remove the password_confirmation
field from the registration form. This disables the need to confirm the password entirely!
- Generate devise views if you haven't:
rails g devise:views
- Remove the
password_confirmation
section inapp\views\devise\registrations\new.html.erb
The reason why this works lies in lib/devise/models/validatable.rb
in the Devise source:
module Devise
module Models
module Validatable
def self.included(base)
base.class_eval do
#....SNIP...
validates_confirmation_of :password, :if => :password_required?
end
end
#...SNIP...
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
end
end
end
Note that the validation is only triggered if password_required?
returns true
, and password_required?
will return false
if the password_confirmation
field is nil
.
Because where the password_confirmation
field is present in the form, it will always be included in the parameters hash , as an empty string if it is left blank, the validation is triggered. However, if you remove the input from the form, the password_confirmation
in the params will be nil
, and therefore the validation will not be triggered.
Rails/Devise---Password Reset & Resend Confirmation How To Redirect to Homepage
- How to change redirect on reset password instructions to go to home page, not user sign in page.
just put this in your passwords
controller
protected
def after_sending_reset_password_instructions_path_for(resource_name)
root_path
end
- How to change redirect on resend confirmation instructions to go to home page, not user sign in page.
create new confirmations_controller.rb
and put this
class ConfirmationsController < Devise::ConfirmationsController
protected
def after_resending_confirmation_instructions_path_for(resource_name)
root_path
end
end
and in routes
devise_for :users, :controllers => { :passwords => "passwords" , :confirmations => "confirmations" }
- How to make it so users can no longer access users/sign_in page
Simple trick is to redirect the user to the home page since your website is in production. remove the path might cause some problem so best is to do this
get 'users/sign_in' => redirect("/")
Side note:
If all you want to signin in root page, you could have used this
root :to => "devise/sessions#new"
update
you have to remove this line from route.rb
file
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
devise_for :users, :controllers => { :passwords => "passwords" , :confirmations => "confirmations" }
get 'users/sign_in' => redirect("/")
and add this
get 'users/sign_in' => redirect("/")
get '/users/sign_out' => 'devise/sessions#destroy'
devise_for :users, :controllers => { :passwords => "passwords" , :confirmations => "confirmations" }
Related Topics
Sorting an Array of Objects in Ruby by Object Attribute
Download All Gems Dependencies
Gem::Loaderror: When Trying to Deploy on Heroku - Rails 4
Numeric Literals Prepended with '0'
On Destroying Session via Devise "Couldn't Find User with 'Id'=Sign_Out"
Why Won't Ternary Operator Work with Redirect
How to Render the Ajax Response in Rails
Rails + Google Calendar API Events Not Created
Regex to Extract Boundary and Content Type Out of Mail Headers
Rally Ruby Toolkit: How to Get Url of Portfolio Item's State
How to Get Order Username and Provisiondate for All Softlayer MAChines Using Ruby
Ruby Before_Validation Triggers Infinite Loop of Call Back
API Post with Array Using Http Gem (Or Restclient)
How to Put a Raw SQL Query in Sequel
How to Check If a Specific Key Is Present in a Hash or Not