Where to put controller UserMailer.signup_confirmation(@user).deliver from Railscast on Action Mailer

StackOverflow https://stackoverflow.com/questions/22971211

Вопрос

I'm following the railscast http://railscasts.com/episodes/61-sending-email-revised which states that I should add

UserMailer.signup_confirmation(@user).deliver

to

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, notice: "Signed up successfully."
  else
    render :new
  end
end

after the line if @user.save ... But what if I am using devise? Do I need to add UserMailer.signup_confirmation(@user).deliver to another place where the Devise gem is hiding the equivalent of a users_controller ?

Это было полезно?

Решение

Without Devise

Since, you are following the RailsCasts episode for Sending Email then in that case all you need to do is update UsersController#create as below:

def create
  @user = User.new(params[:user])
  if @user.save
    UserMailer.signup_confirmation(@user).deliver  ## Add this
    redirect_to @user, notice: "Signed up successfully."
  else
    render :new
  end
end

With Devise

Option #1 Sending a welcome confirmation mail

What you can do here is, override the after_sign_in_path_for method in ApplicationController as below:

class ApplicationController < ActionController::Base
  ## ...
  protected

  def after_sign_in_path_for(resource)
    UserMailer.signup_confirmation(@user).deliver  ## Add this
    your_path(resource)  ## Replace your_path with the path name where you want to send the user after sign in 
  end
end 

Option #2 Sending a welcome confirmation mail with confirmation token

For this you will need to use Devise built-in Confirmable module.

Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.

Refer to Confirmable Documentation

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top