There's no callbacks for ActionMaler::Base in Rails 3
undefined method `before_action' for ActionMailer
-
02-06-2022 - |
Question
Trying to attach file to mail via before_action filter:
class UserMailer < ActionMailer::Base
before_action :add_logo_attachment
layout 'mail'
default from: "\"Admin\" <admin@site.com>",
to: Proc.new {Admin.pluck(:email)}
def send_mail
mail(subject: 'Hello, admin!')
end
.
.
private
def add_logo_attachment
attachments.inline['logo.png'] = File.read(Rails.root.join('app/assets/images/logo.png'))
end
end
And I get this error: undefined method `before_action' for UserMailer:Class There is the same example in Rails guides and I can't understand what's the difference between my code and the code in guides.
Solution
OTHER TIPS
Check the beginning of your controller. Had a similar error where this...
before_action :set_org, only: [:show, :edit, :update, :destroy]
got moved to above this accidently...
class OrgsController < ApplicationController
Move it back inside the class.
Try to
include AbstractController::Callbacks
in your mailer class and use before_filter
See this topic
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow