Rails ActionMailer User Mailer No Method - ruby-on-rails-5

I'm not geting my user_mailer work. It says there's no method and I coudn't find out why:
From Rails:
NoMethodError in AtendimentosController#create
undefined method `welcome_email' for UserMailer:Module
From Rails C (manual test):
irb(main):027:0> ActionMailer::usermailer.send_welcome_email(from: "test#example.co", to: '...#gmail.com', subject: 'subjecto de teste', body: "Test")
Traceback (most recent call last):
2: from (irb):27
1: from (irb):27:in `rescue in irb_binding'
NoMethodError (undefined method `usermailer' for ActionMailer:Module)
class UserMailer is located at /mailers/user_mailer/user_mailer.rb :
class UserMailer < ApplicationMailer
def welcome_email(user)
mail(:to => #user.email, :subject => "Welcome!")
end
end
I'm calling at a controller:
def send_mail
UserMailer.welcome_email(self).deliver_later
end

You should move this class to /mailers/user_mailer.rb. When you put the file in a separate folder, it is required that the file begins with the Module ModuleName. So in your example, when you put this file in /mailers/user_mailer/user_mailer.rb it should look like this:
module UserMailer
class UserMailer < ApplicationMailer
def welcome_email(user)
mail(:to => #user.email, :subject => "Welcome!")
end
end
end
And then you call it: UserMailer::UserMailer.welcome_email(self).deliver

Related

undefined method `before_action' for ActionMailer

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.
There's no callbacks for ActionMaler::Base in Rails 3
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

Issue with helper_methods from views rails

I have a very frustrating issue.
I can't call any helper method from my views in rails.This is what I have:
ApplicationController.rb
class ApplicationController < ActionController::Base
protect_from_forgery
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
helper_method :all
end
/app/views/welcome/_navi_bar.haml:
%ul.nav.nav-pills.pull-right
%li.pull-right
- if current_user
%a.btn-small{"data-toggle" => "modal", :href => log_out_path, :role => "button"}
%i.icon-user.icon-white
%strong
Log Out
- else
%a.btn-small{"data-toggle" => "modal", :href => "#LoginBox", :role => "button"}
%i.icon-user.icon-white
%strong
Login
This is what I get as error:
undefined local variable or method `current_user' for #<#<Class:0x007ff0a0544668>:0x007ff0a05414b8>
I really don't get what the problem is. Please help !
You have written your code in application controller instead of application helper
That is the reason why your method is not getting called
if you want to check if current user is logged in or not
you may just use before filter in application controller and call the method
whenever you dont need to check the method add skip before filter in that place

how can you test the 'catch all route' using ordinary controller tests?

Note: As per RafaeldeF.Ferreira's suggestion, this question has been heavily edited since its original form.
My JSON-based app needs to return something sensible when given a bad route. We already know that the following rescue_from ActionController::RoutingError doesn't work in Rails 3.1 and 3.2:
# file: app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from ActionController::RoutingError, :with => :not_found
...
end
(This is well documented in https://github.com/rails/rails/issues/671.) So I implemented what José Valim describes in this blog entry (item 3), and details are provided below.
But testing it has been problematic. This controller rspec test:
# file: spec/controllers/errors_controller.rb
require 'spec_helper'
require 'status_codes'
describe ErrorsController do
it "returns not_found status" do
get :not_found
response.should be(StatusCodes::NOT_FOUND)
end
end
fails with:
ActionController::RoutingError: No route matches {:format=>"json", :controller=>"sites", :action=>"update"}
Yet this integration test calls ErrorsController#not_found and succeeds:
# file: spec/requests/errors_spec.rb
require 'spec_helper'
require 'status_codes'
describe 'errors service' do
before(:each) do
#client = FactoryGirl.create(:client)
end
it "should catch routing error and return not_found" do
get "/v1/clients/login.json?id=#{#client.handle}&password=#{#client.password}"
response.status.should be(StatusCodes::OK)
post "/v1/sites/impossiblepaththatdoesnotexist"
response.status.should be(StatusCodes::NOT_FOUND)
end
end
So: Is there a way to test the 'catch all route' using ordinary controller tests?
implementation details
If you want to see the implementation, here are the relevant code snippets
# config/routes.rb
MyApp::Application.routes.draw do
... all other routes above here.
root :to => "pages#home"
match "/404", :to => "errors#not_found"
end
# config/application.rb
module MyApp
class Application < Rails::Application
config.exceptions_app = self.routes
...
end
end
# config/environments/test.rb
MyApp::Application.configure do
...
config.consider_all_requests_local = false
...
end
# app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render :json => {:errors => ["Resource not found"]}, :status => :not_found
end
end

NoMethod Error when using the mail() method in a Rails 3.0.3 ActionMailer

I am trying to create a mailer class for a RoR webapp I am working on for a class. I followed the rails guides steps online, but when I try to call my ActionMailer I get a NoMethodError: undefined method `mail' for MyMailer:Class.
Here's my code for MyMailer
def MyMailer.immediate_notification_mail(r, st)
#url = "myurl"
st.each do |s|
mail(:to => s.email, :subject => "Subject")
end
end
I call this method from another class with the folowing line of code:
MyMailer.immediate_notification_mail(rp, st).deliver
Can anyone tell me what I might be doing wrong? I haven't seen anyone else with this problem.
Thanks!!
The issue is you are setting up a static (class) method.
def MyMailer.immediate_notification_mail(r, st)
This should be:
def immediate_notification_mail(r, st)

Rails 3 rescue_from, with and working with custom modules

I am writing a Rails app that I am wanting to DRY up just a tad bit and instead of calling my custom error class at the top of each controller I need it in, I placed it inside of a Module and just included that module.
Working code (Module):
module ApiException
class EmptyParameter < StandardError
end
end
Working code (Controller):
# include custom error exception classes
include ApiException
rescue_from EmptyParameter, :with => :param_error
# rescure record_not_found with a custom XML response
rescue_from ActiveRecord::RecordNotFound, :with => :active_record_error
def param_error(e)
render :xml => "<error>Malformed URL. Exception: #{e.message}</error>"
end
def active_record_error(e)
render :xml => "<error>No records found. Exception: #{e.message}</error>"
end
Here is my question, using the :with command, how would I call a method inside my custom module?
Something like this: rescue_from EmptyParameter, :with => :EmptParameter.custom_class
You could try something like this:
rescue_from EmptyParameter do |exception|
EmptyParameter.custom_class_method
end