I get this error after visit http://localhost:3000/rails/mailers/user_mailer/confirm_email:
undefined method '[]' for nil:NilClass
#user = params[:user]
My code:
class UserMailer < ApplicationMailer
def confirm_email
#user = params[:user]
mail to: "to#example.org"
end
end
class UserMailerPreview < ActionMailer::Preview
def confirm_email
UserMailer.with(user: User.first).confirm_email
end
end
If I change code to this:
class UserMailer < ApplicationMailer
def confirm_email(user)
#user = user
mail to: "to#example.org"
end
end
class UserMailerPreview < ActionMailer::Preview
def confirm_email
UserMailer.confirm_email(User.last)
end
end
I receive this:
wrong number of arguments (given 0, expected 1)
I cant understand, what I'm doing wrong?
My rails version is 6.0.2.1
Update:
First case worked from console, but not worked from browser preview. Second case not worked from console.
I was able to resolve this same problem by removing the Letter Opener gem from my project. Letter Opener has similar functionality to the now built-in ActionMailer previews.
Below is the normal format I copied from http://api.rubyonrails.org/
class ApplicationMailer < ActionMailer::Base
default from: 'from#example.com'
layout 'mailer'
end
But what I want is to be able to have multiple address that it send form.
I tried to call a method, for example:
class ApplicationMailer < ActionMailer::Base
default from: address
layout 'mailer'
def address
Apartment::Tenant.current_tenant == "org" ? "custom#email.com" : "from#example.com"
end
end
When I call that method it returns
<ActionMailer::Base::NullMail:0x007fbefe0eb388>
and not the string that I want.
Try below code:
class ApplicationMailer < ActionMailer::Base
default from: Apartment::Tenant.current_tenant == "org" ? "custom#email.com" : "from#example.com"
layout 'mailer'
end
I'm trying to get access to some of my application_helper methods within my mailer, but nothing seems to be working from these SO posts:
View Helpers in Mailers
Access Helpers from mailer
In app/helpers/application_helper.rb I have the following:
module ApplicationHelper
def get_network_hosts
.. stuff get get a #network_hosts object
end
end
In my mailer at app/mailers/user_notifier.rb I have the following:
class UserMailer < ActionMailer::Base
default from: "Support <support#me.co>"
add_template_helper(ApplicationHelper)
def trial_notifier(user)
get_network_hosts
#user = user
#total = user.company.subscription.per_device_charge * #network_hosts.count
if #total < 501
#message = "You'd pay #{#total}/month if you converted to full-access now!"
else
#message = "You'd pay #{#total}/month if you converted to full-access now, but we have a better deal for you!"
end
#url = edit_user_registration_url
mail(to: #user.email, subject: 'What's up?')
end
end
In my mailer I've tried all of the suggestions in the above SO posts, but I'm still getting this error:
`NameError: undefined local variable or method `get_network_hosts' for #<UserMailer:0x007fe756c67c18`>
I'm currently using Rails 4.1.7.
So what do I have to actually do to be able to use my application_helper methods within a mailer?
You can try to do this as following:
In your mailer at app/mailers/user_notifier.rb:
class UserMailer < ActionMailer::Base
default from: "Support <support#me.co>"
helper :application
or you can try this:
helper ApplicationHelper
I'm using whenever to set up a mailer but I'm getting the following error in my cron log:
/.rvm/gems/ruby-2.1.0/gems/railties-4.0.0/lib/rails/commands/runner.rb:53:in `eval': undefined method `unreadmailer' for #<Class:0x00000101b7a5c8> (NoMethodError)
I call my method in the schedule.rb as follows:
every 5.minutes do
runner "Message.unreadmailer"
end
and in my message.rb model class I have the method defined as follows:
class Message < ActiveRecord::Base
def self.unreadmailer
#message = Message.all
#message.each do |m|
if m.receiver == 0 && m.readStatus == 0
messageMailer.message_email(m.belongs_to).deliver
end
end
end
why is it an undefined method?
my mailer is defined as follows (I don't think it has to do with the mailer because the website has a mailer not linked with whenever that works)
class MessageMailer < ActionMailer::Base
ADMIN = 'xxxx#xx.edu'
def message_email(patient)
#patient = patient
mail(to: ADMIN, subject: 'New message at xxxxx.xxx.edu')
end
end
I have small organizatoric issue, in my application I have 3 mailer User_mailer, prduct_mailer, some_other_mailer and all of them store their views in app/views/user_mailer ...
I will want to have a subdirectory in /app/views/ called mailers and put all in the folders user_mailer, product_mailer and some_other_mailer.
Thanks,
You should really create an ApplicationMailer class with your defaults and inherit from that in your mailers:
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
append_view_path Rails.root.join('app', 'views', 'mailers')
default from: "Whatever HQ <hq#whatever.com>"
end
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def say_hi(user)
# ...
end
end
# app/views/mailers/user_mailer/say_hi.html.erb
<b>Hi #user.name!</b>
This lovely pattern uses the same inheritance scheme as controllers (e.g. ApplicationController < ActionController::Base).
I so agree with this organization strategy!
And from Nobita's example, I achieved it by doing:
class UserMailer < ActionMailer::Base
default :from => "whatever#whatever.com"
default :template_path => '**your_path**'
def whatever_email(user)
#user = user
#url = "http://whatever.com"
mail(:to => user.email,
:subject => "Welcome to Whatever",
)
end
end
It is Mailer-specific but not too bad!
I had some luck with this in 3.1
class UserMailer < ActionMailer::Base
...
append_view_path("#{Rails.root}/app/views/mailers")
...
end
Got deprecation warnings on template_root and RAILS_ROOT
If you happen to need something really flexible, inheritance can help you.
class ApplicationMailer < ActionMailer::Base
def self.inherited(subclass)
subclass.default template_path: "mailers/#{subclass.name.to_s.underscore}"
end
end
You can put the templates wherever you want, but you will have to specify it in the mailer. Something like this:
class UserMailer < ActionMailer::Base
default :from => "whatever#whatever.com"
def whatever_email(user)
#user = user
#url = "http://whatever.com"
mail(:to => user.email,
:subject => "Welcome to Whatever",
:template_path => '**your_path**',
)
end
end
Take a look at 2.4 Mailer Views for more info.
Easy Solution: Specify Path in ApplicationMailer
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
prepend_view_path "app/views/mailers" # <---- dump your views here
end