Devise Sending Welcome Email - ruby-on-rails

I have a devise model called members i am using devise confirmable. Upon confirm i want to send a welcome email to the User
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Methods
# Override devise confirm! message
def confirm!
welcome_email
super
end
# Private Methods
private
def welcome_email
MemberMailer.welcome_email(self).deliver
end
end
My MemberMailer resides in mailers/brands/member_mailer.rb
class Brands::MemberMailer < ApplicationMailer
# Send Welcome Email once Member confirms the account
def welcome_email(member)
#member = member
mail(to: #member.email, subject: "Welcome to Skreem! Now you Rock!")
end
end
But upon confirming through the mail Link the confirm! is not being overridden and I am not getting any error or email.

Add this to your Member model:
def after_confirmation
welcome_email
end
For more info check after_confirmation
#Pavan thanks for pointing this.
Your welcome_email should be:
def welcome_email
Brands::MemberMailer.welcome_email(self).deliver
end

Related

redirect with google_oauth2 on validation

I got a validation on my model to allow only some emails to log in with GoogleOauth but for some reasons, it does not redirect me to the root path
I got my admin Model :
class Admin < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :confirmable, :lockable, :registerable, :rememberable, :recoverable, :timeoutable,
:trackable, :validatable
devise :omniauthable, omniauth_providers: [:google_oauth2]
validates :email, :test_email
def self.from_omniauth(auth)
admin = Admin.find_or_initialize_by(email: auth.info.email)
admin.password = Devise.friendly_token.first(12)
admin.skip_confirmation!
admin.save!
admin
end
private
def test_email
errors.add(:email, message: 'Email is not correct') unless email.match?(/\A[a-z.\-]+(\+\d+)?#test\.fr?\z/)
end
end
And my OmniauthCallbacksController :
def google_oauth2
#admin = Admin.from_omniauth(request.env['omniauth.auth'])
if #admin.persisted?
flash[:notice] = 'success'
sign_in_and_redirect #admin
else
session['devise.google_data'] = request.env['omniauth.auth'].except('extra')
flash[:error] = #admin.errors.full_messages.join("\n")
redirect_to root_path
end
end
end
validates expects a boolean return. Just adding an error is not enough.
Try this:
def test_email
return true unless email.match?(/\A[a-z.\-]+(\+\d+)?#test\.fr?\z/)
errors.add(:email, message: 'Email is not correct')
false
end

Devise sending welcome email after confirmation

I'm using rails 5.2 and devise 4.4.3, I have confirmable working, however, I'm trying to have it so when the user confirms their account they get sent another email welcoming them to the website.
On sign up the confirmation email sends and confirmation can take place by clicking the link in the email but the welcome email that I want to be sent after this currently doesn't send.
The code I have is like so, I have generated a UserMailer and done the following
user.rb model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one_attached :avatar
def confirm!
welcome_email
super
end
protected
def welcome_email
UserMailer.welcome_email(self).deliver
end
end
views/user_mailer/welcome_email.html.erb
<h2>Welcome <%= #user.email %></h2>
mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! You are awesome!")
end
end
You can use the after_confirmation callback instead of trying to override confirm.
def after_confirmation
UserMailer.welcome_email(self).deliver
end
def after_confirmation
UserMailer.with(user: #user, password: #user.password).welcome_email.deliver_later
end

undefined method `current_user' ( commontator + devise )

I am using devise & commontator gems
i don't know what the error about "current_user" when try to "show" the product !!
undefined method `current_user' for #<ProductsController:0xd09600c>
user model
class RegisteredUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Registered user can comment on a product
acts_as_commontator
end
product model
class Product < ActiveRecord::Base
# Product can be commented on
acts_as_commontable
end
i call show method from ProductsController
class ProductsController < ApplicationController
def show
commontator_thread_show(#product)
end
end
the devise helper based on model name
so it is current_registered_user not current_user
have to change it in commontator initializer file then restart the server

creating sign in only views/methods

I created a user model using devise. I was wondering how can I create views and methods that only work after I've signed in?
Here is my model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:rememberable, :validatable
end
controller:
class UserController < ApplicationController
def create
User.create(user_params)
end
private
def user_params
# required input for params
# permit - returns a version of the params hash with ony the permitted attributes
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
You will be protecting controllers and it's actions.
before_action :authenticate_user!
user_signed_in?
current_user
user_session
read more!
https://github.com/plataformatec/devise

devise_invitable: Confirm after Invitation

I override devise's confirm! method to send a welcome message to my users:
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
# ...
# Devise confirm! method overriden
def confirm!
UserMailer.welcome_alert(self).deliver
super
end
end
With devise_invitable when the user accept the invitation and set his password the confirm! method is never triggered, is it possible to force it? How does devise_invitable confirms the User?
Or maybe I can override the accept_invite (or whatever its called) method the same way?
I want that invited users remain unconfirmed, and then confirmed upon accepting the invitation.
Thanks, any help very appreciated!
Original Source
UPDATE
Looking through devise_invitable model I found the two methods who may be causing this misbehavior:
# Accept an invitation by clearing invitation token and confirming it if model
# is confirmable
def accept_invitation!
if self.invited? && self.valid?
self.invitation_token = nil
self.save
end
end
# Reset invitation token and send invitation again
def invite!
if new_record? || invited?
#skip_password = true
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
generate_invitation_token if self.invitation_token.nil?
self.invitation_sent_at = Time.now.utc
if save(:validate => self.class.validate_on_invite)
self.invited_by.decrement_invitation_limit! if self.invited_by
!!deliver_invitation unless #skip_invitation
end
end
end
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
# ...
# devise confirm! method overriden
def confirm!
welcome_message
super
end
# devise_invitable accept_invitation! method overriden
def accept_invitation!
self.confirm!
super
end
# devise_invitable invite! method overriden
def invite!
super
self.confirmed_at = nil
self.save
end
private
def welcome_message
UserMailer.welcome_message(self).deliver
end
end
I tried benoror's answer and at first it appeared to work - but when you a user accepts the invitation and fills in the form as invalid it will actually override the token invalidating the invitation.
Instead, a callback is available to do this:
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
after_invitation_accepted :send_welcome_email
def send_welcome_email
end
end

Resources