I'm trying to follow this wiki to have the admin approve requests for registration.
https://github.com/plataformatec/devise/wiki/How-To%3a-Require-admin-to-activate-account-before-sign_in
When I try to complete the sign up form, I get this error when I press the sign up button:
NameError at /users
uninitialized constant User::AdminMailer
It refers to line 96 in my user model. That is where this method is:
def send_admin_mail
AdminMailer.new_user_waiting_for_approval(self).deliver
end
I have a after action for send_admin_email.
class UserMailer < ActionMailer::Base
default from: "hello#cr.com"
def send_admin_mail
mail(to: hello#cr.com, subject: 'Registration Request')
end
end
Any ideas as to what I'm doing wrong?
Thank you.
your class is called UserMailer, but you're creating an instance of AdminMailer. Maybe try renaming one or the other. The tutorial suggests the class should be called AdminMailer.
Related
In my application I have users and sites (think of it like users and organizations). There's a lookup table the sits between them called SiteUser. In SiteUser:
belongs_to :site
belongs_to :user
before_validation :set_user_id, if: ->() { email != nil }
def set_user_id
existing_user = User.find_by(email: email)
self.user = if existing_user.present?
UserMailer.notify_existing_user(site, existing_user).deliver_now unless Rails.env.test?
existing_user
else
User.invite!(email: email)
end
end
I need the subject of the email that gets generated to include the site name. "Example Company has invite you to join their site!"
And in the email body I also need the site title.
I am confused on how to get the site parameter over to devise invitable. As you can see in the code above, if the user who is being invited to the site already exists in our system, I use my own mailer in which I pass in the site and the existing_user so I have access to it in my mailer view.
class UserMailer < ApplicationMailer
def notify_existing_user(site, user)
#site = site
#user = user
mail to: #user.email, subject: "You've been given access to #{#site.title} in the Dashboard."
end
end
I cannot figure out how to accomplish this similarly with the devise invitable mailer, which is used when the user doesn't already exist in the system.
Any help you could provide would be incredibly appreciated!
The way I went about accomplishing this was to add a temporary/virtual attribute on User called invite_site_name. Because the problem was that site wasn't an attribute of User, so I could not send site in to User.invite!.
class User < ActiveRecord::Base
attr_accessor :invite_site_name
and then in my CustomDeviseMailer I had access to it:
class CustomDeviseMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
def invitation_instructions(record, token, opts={})
opts[:subject] = "#{record.invite_site_name} has invited you to their Dashboard! Instructions inside!"
super
end
end
You could always override devise's subject_for method. Found this on the gems issues, that also suggests another way:
https://github.com/scambra/devise_invitable/issues/660#issuecomment-277242853
Hope this helps!
I have this weird thing going on in my rails4 app:
I created event.rb in the lib folder.
In there, I call a mailer:
def whatever
puts 'here'
UserMailer.welcome(user)
puts 'there'
end
which is calling
class UserMailer < ActionMailer::Base
def welcome(user)
#user = user
mail(to: #user.mailer, subject: 'Welcome to my app').deliver
end
end
The weird thing is that the method welcome is never called, while whatever is called, without raising any error (the logs are there).
But if I call UserMailer.welcome(User.first) in the console, it is sent.
What am I doing wrong? Is it that it is not possible to send an email from a module? I should move this code to a model? That would be weird.
Thanks in advance
IMO mailer should look like this:
class UserMailer < ActionMailer::Base
def welcome(user)
#user = user
mail(to: #user.mailer, subject: 'Welcome to my app') #.deliver removed
end
end
and should be invoked with this manner:
def whatever
puts 'here'
UserMailer.welcome(user).deliver_now # and added here
puts 'there'
end
I am building an rails based e-commerce application first time. I want to send an email to user when he submits the order, like the way we get receipt on email when we place order on e.g. mealnut.com or fab.com. I was searching for tutorials but not getting related to order submit emails. Every where user sign up or reset etc.
Has any one implemented it? or know any resource/tutorial in rails?
Your guidance/help will be appreciated!
First generate the mailer for writing required actions.
rails g mailer UserMailer
then in app/mailers/user_mailer.rb file
class UserMailer < ActionMailer::Base
default from: 'notifications#example.com'
def order_confirmation(user, order)
#user = user
#order = order
mail(to: user.email, subject: 'Order has been received')
end
end
and the view content for the email would be like this app/views/user_mailer/order_confirmation.html.erb
Hi <%= #user.name %>
You have successfully placed an order with us.
Please find the details of order....
#.............
then in the controller action, where you will create a order, place the below line after creating the order to send an email
UserMailer.order_confirmation(user, order).deliver
Go through the action mailer tutorial for more information.
I'm afraid sending an email is a fairly standard procedure.. and the tutorials you've found are probably applicable. You need to understand that triggering a message to be sent can be done from any controller action.. in your case you'll want the order create action.
After reading the following:
http://railscasts.com/episodes/61-sending-email-revised?view=asciicast
You can make the necessary changes and call the mailer from your order create action:
class OrdersController < ApplicationController
def create
#order = Order.new(params[:order])
if #order.save
UserMailer.order_confirmation(#order, #user).deliver
redirect_to #user, notice: "Order Completed Successfully."
else
render :new
end
end
end
The reason I am using UserMailer in the above is because you will likely want to set up a mailer that sends messages to Users, but you could call it OrderMailer if you wanted.
You cannot/not suitable use Devise mailer (since devise mailer is for User authentication purposes). What you could do is, you could use a observer class to send e-mails
Ex:
class Order < ActiveRecord::Base
#your standard order code
end
class OrderObserver < ActiveRecord::Observer
def after_create(order)
#Email sending code
end
end
This OrderObserver sends an email when a Order#create is finished. Read more about observer class
Regarding sending email with rails3 check this, and its same as sending emails for forgotpassword / signup etc, it's just that content is different.
I'm trying to send an email upon user registration in a rails 3 application that is using the devise gem. Every time I try to the send the email I get the following error:
NoMethodError in Devise::RegistrationsController#create
undefined method `welcome_email' for UserMailer:Class
My app/model/user.rb has the following code...
after_create :send_welcome_email
def send_welcome_email
UserMailer.welcome_email(self).deliver
end
My app/mailers/user_mailer.rb has the following code...
class UserMailer < ActionMailer::Base
default from: "support#mysite.com"
def welcome_email(user)
#user = user
#url = "http://mysite.com/login"
mail(:to => "#{user.email}", :subject => "Welcome to My Awesome Site")
end
end
The method welcome_email exists so I'm not sure why I'm getting the error. Been trying to resolve this problem for the past couple of hours.
Thanks in advance for you help!! As always if you give me a good answer I will accept it as so.
Alex
Looks like the welcome_email is an instance method. And it looks like the error says it needs to be a class method. So try rewriting the method declaration as:
def self.welcome_email(user)
I guess that should solve it.
All of my users will be unapproved until they are approved by an admin, the admin will be logging into the site to mark the user as approved. I am following the Devise docs here which is working out great but how do I send an email to the admin once a new user has signed up so that the admin is aware and can approve the sign up?
How about in your User model, do something like this:
after_create :send_admin_mail
def send_admin_mail
###Send email stuff here
end
You may want to use ActionMailer.
There may be some built in Devise way, but I can't find anything. This basically just sends an alert to you.
As of 2020
The docs have been updated to answer this question here. Of course feel free to modify to your needs.
Summary:
After running rails g mailer AdminMailer. Modify app/mailers/admin_mailer.rb to the following:
class AdminMailer < Devise::Mailer
default from: 'from#example.com'
layout 'mailer'
def new_user_waiting_for_approval(email)
#email = email
mail(to: 'admin#email.com', subject: 'New User Awaiting Admin Approval')
end
end
Then update app/models/user.rb to:
after_create :send_admin_mail
def send_admin_mail
AdminMailer.new_user_waiting_for_approval(email).deliver
end
Lastly, modify the following to your needs views/admin_mailer/new_user_waiting_for_approval.erb.