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
Related
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.
I have set a mailer:
def created_poll_email(poll)
#poll = poll
mail(subject: 'A new poll was created! ' + #poll.question)
end
Firstly I was calling it on polls controller:
def create
#poll = Poll.new(poll_params.merge(user: current_user))
if #poll.save
PollMailer.created_poll_email(#poll).deliver_now
And it was working fine.
Now I want to move it to a callback on model:
after_save :send_email
def send_email
PollMailer.created_poll_email(#poll).deliver_now
end
But now i get error undefined method `question' for nil:NilClass. I tried to set also other callbacks as after_create or after_commit but same result.
Why is this happening and how can I fix?
You should replace #poll, which isn't set inside your model, with self:
def send_email
PollMailer.created_poll_email(self).deliver_now
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 have an after_create callback in my Tag model:
def auto_vote
params = parametrize_media_tag(media_tag)
Tag::Vote.cast_vote(params)
end
Which gives me this error:
undefined method `cast_vote' for #<Class:0x7ae7a90>
My Tag::Vote model is quite simple:
class Tag::Vote < Vote
def self.cast_vote(params)
Vote.cast_vote_of_type(params, self.class.name)
end
end
Why isn't Rails detecting the cast_vote method?
In my Rails application I want to temporarily stop sending email for specific users (e.g. when I get bounces due to quota) until the user confirms he is able to receive email again.
I have a common superclass for all mailer classes. There I always call a method setup_email before sending a mail.
Where is the best place to call #user.mail_suspended??
Here is some simplified sample app, I use Rails 2.3:
# Common super class for all Mailers
class ApplicationMailer < ActionMailer::Base
protected
def setup_mail(user)
#recipients = user.email
#from = ...
end
end
# Specific Mailer for User model
class UserMailer < ApplicationMailer
def message(user, message)
setup_mail(user)
#subject = "You got new message"
#body[:message] = message
end
end
# Use the UserMailer to deliver some message
def MessagesController < ApplicationController
def create
#message = Message.new(params[:message])
#message.save
UserMailer.deliver_message(#message.user, #message)
redirect_to ...
end
end
I solved this by setting the ActionMailer::Base.perform_deliveries to false:
def setup_mail(user)
email = user.default_email
if email.paused?
ActionMailer::Base.perform_deliveries = false
logger.info "INFO: suspended mail for #{user.login} to #{email.email})"
else
ActionMailer::Base.perform_deliveries = true
end
# other stuff here
end
I wouldn't set perform_deliveries universally, just per message, e.g.
after_filter :do_not_send_if_old_email
def do_not_send_if_old_email
message.perform_deliveries = false if email.paused?
true
end
I tried many ways, but no one could help me except this one.
class ApplicationMailer < ActionMailer::Base
class AbortDeliveryError < StandardError; end
before_action :ensure_notifications_enabled
rescue_from AbortDeliveryError, with: -> {}
def ensure_notifications_enabled
raise AbortDeliveryError.new unless <your_condition>
end
...
end
Make a class inherited with standardError to raise exception.
Check the condition, if false then raise exception.
Handle that exception with the empty lambda.
The empty lambda causes Rails 6 to just return an
ActionMailer::Base::NullMail instance, which doesn't get delivered
(same as if your mailer method didn't call mail, or returned
prematurely).