Devise how to pass current_user to confirm_instructions mailer
devise, ruby-on-rails
Solution
Follow this tutorial for setting up a custom mailer.
In config/initializers/devise.rb:
config.mailer = "UserMailer".
Create a new mailer that inherits from Devise's mailer in the folder app/mailers:
# user_mailer.rb
class UserMailer < Devise::Mailer
def invite(sender, recipient)
@sender = sender
@recipient = recipient
mail( :to => recipient.email,
:subject => "Invite by #{sender.name}"
)
end
end
Now, move your devise mailer views to the folder app/views/user_mailer. There create a new email view where you can use the variables @sender and @recipient.
# invite.html.erb
<p>Welcome <%= @recipient.email %>!</p>
<% if @sender.email? %>
<p> some additional welcome text here from <%= @sender.email %> </p>
<% end %>
Now, in your controller, you can call the following:
UserMailer.invite(current_user, newContact).deliver
Problem
I have Devise set up and woking great. I am using confirmable and have modified this as per their 2 step registration process guide: set password at confirmation I have one last requirement that I am having trouble with. What we have is a 2 scenarios 1) a user can register as new 2) A logged in user (current_user) can create a new user. When a logged in user creates a new user I want to be able to add their email into the confirmation email sent to the new created user In the email to the new registered user I need to pass in the current_user.email somehow if the user was created by a user one logged in. I will then do a simple if check and add some extra text to the email. the confirmation_instructions.html.erb currently: ``` <p>Welcome <%= @resource.email %>!</p> <p>You can confirm your account email through the link below:</p> <p><%= link_to 'Confirm account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p> ``` What I need is something like ``` <p>Welcome <%= @resource.email %>!</p> <% if !@user.email.nil? %> <p> some additional welcome text here from <%= @user.email %> </p> <% end %> <p>You can confirm your account email through the link below:</p> <p><%= link_to 'Confirm account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p> ``` I have been going back and forth with custom mailer with no joy. Can someone help me out, I am sure there is something simple I am missing here. For info (I know this is not the best method but we are throwing together a very quick app for demo purposes) a user creates a new contact by typing in am email address. If the email address does not exist in the user table a new user is created then the contact relationship created (snippet of the controller): ``` class DashboardController < ApplicationController before_filter :authenticate_user! def show @contacts = current_user.contacts end def createcontact user2 = User.find_by_email(params[:contact_email]) if user2.nil? newContact = User.create(:email => params[:contact_email]) if newContact.save current_user.newUserContact(newContact) redirect_to dashboard_path, :notice => "conact has been saved as well as a new contact" else redirect_to dashboard_path, :notice => "ERROR saving contact" end else . . . . ```