My Controller can't find my (Action)Mailer! - ruby-on-rails

Greeting all...
I'm trying to figure out a problem that I've never seen and shouldn't be happening by all accounts...
I'm using Rails 3.0.12 with a simple/standard ActionMailer setup:
I have HelpRequestMailer in app/mailers
I have HelpRequestsController in app/controllers
First problem... When I call the mailer in my controller...
help_request = HelpRequest.new(params[:help_request])
...
HelpRequestMailer.help_request_email(help_request).deliver
I get the following error: NameError (uninitialized constant HelpRequestsController::HelpRequestMailer)
This in itself is strange.
When I add the following...
require File.expand_path('../../mailers/help_request_mailer', FILE)
...To the top of the controller (I would expect the path to be '../mailers/help_request_mailer' but that doesn't work) - which I shouldn't have to do - the controller seems to find the mailer but doesn't seem to recognize what it is/know what to do with it. I get the following error:
NoMethodError (undefined method `help_request_email' for HelpRequestMailer:Class)
Which is technically true... There's no help_request_email class method in HelpRequestMailer... It's an instance method (as specified by the documentation).
My ActionMailer configuration lives in config/application.rb
Any help/suggestions would be greatly appreciated...

I had this problem too, and it turned out to be a case of a completely unhelpful error message. In my case it was just a syntax error in some of my code for the action mailer or the associated view.
I think what happened is that when Rails couldn't properly parse the mailer or view code, it just bypassed the files and never instantiated the action mailer object, leading to the error in the controller.
I would have much preferred it if Rails had tripped on the error in the action mailer code itself.

Related

Getting "Uninitialized constant" routing error when loading a view

I'm a beginner to both ruby and rails, and using Rails 5.17 to develop a web app for a class.
Creating the empty Rails project was successful, but something is going wrong when creating a new controller. I generated a new controller named cars from the root of the project, which was successful. There was a file in app/controllers named cars_controller.rb which looks like this:
class CarsController < ApplicationController
end
I added a method to this file named hello that does nothing.
I then created a file named cars.html.erb in the app/views/layouts directory. This file is a basic page of html code.
In config/routes.rb, I added the following:
get '/cars', to:: 'cars_controller#hello'
resources: cars
After all of this, I ran rails server, and opened localhost:3000 in a browser. This brings up the normal Ruby on Rails welcome page.
But when I go to localhost:3000/cars, I get the following:
Routing Error
uninitialized constant CarsControllerController
I've tried changing the name of the cars_controller.rb file. I've tried changing the name of the class in the controller file from CarsController to Cars. I've tried many different routes in routes.rb. I finally tried uninstalling Rails 5.17 and installing Rails 5.13.
I'm very confused, and I'd be grateful for any advice I can get. Thanks in advance!
One of the great things about Rails is its preference for convention over configuration. However, for this to really benefit you, you need to stick to doing things “The Rails Way” rather than your own way, wherever possible.
In this case, start by getting rid of your custom get route, and just use resources :cars.
From the command line, run rake routes (you might be able to run rails routes on your rails version too) and see the routes that it has created for you.
Now, rename the method you added to your CarsController from hello to index.
Move your hello.html.erb file from app/views/layout to app/views/cars/index.html.erb.
Finally, start the rails server (rails start) and load the url http://localhost:3000/cars in your browser.
—-
Note that templates in app/views/layout have a special purpose. These are used to apply a general template to your views. Look up the use of layout within a controller for more details
I think you have an error in how you had defined your route - you don't need _controller.
Instead, try this:
get '/cars', to: 'cars#hello'
Also, keep in mind that in your cars directory you need the view: hello.html.erb

Put gem class into namespace

To my Rails Gemfile I added a new gem. It is used like this:
Email::Client
The issue I'm having right now is that I also have a class in my rails app that's called Email. Now sometimes when I try to initialize it I get the following error:
Email.new # >> undefined method 'new' for Email:Module
Probably because Email is a Module in the gem. How can I fix this issue? Is there a way to namespace the gem module? I don't want to rename my Email class.
I don't want to rename the Email-Class.
Yet this is what you'll have to do. Your code is the only thing you control. Either rename/alias it (MyEmail) or namespace it (MyApp::Email).
Btw, you got off easy this time. Imagine what'd happen if the other Email was also a class. Suddenly, all your methods are gone. You add or change a method and your email doesn't see it. This could make for a frustrating debugging session.

Ruby on Rails - undefined local variable or method - wice grid haml show_code

I am new to ruby on rails.
I am trying to get haml and wice_grid to work together. I am using this example as a model:
http://wicegrid.herokuapp.com/basics3
I get the error 'undefined local variable or method `show_code' for...'
In the file app/views/basics3/index.html.haml which you can see at the link above.
Am I missing a gem? In general, what is the best way to troubleshoot problems like this?
Thanks in advance-
Flex
EDIT: I found the definition for show_code. It's in a helper that I found in the unit tests for wice_grid.
https://github.com/leikind/wice_grid_testbed/blob/master/app/helpers/application_helper.rb
That said, I get more errors when I load it into my project. So the question becomes, how does the helper normally get included in my project?
show_code is a custom method created just for the example page you linked to. It just displays the code he has in his controller and his index and grid views. You don't need to call that method in your own application so just remove that line and you should be good.

What is AbstractController?

In my Rails 3 production.log I see some errors like:
AbstractController::ActionNotFound (The action 'images' could not be found for ClientsController)
I don't notice anything going wrong. I searched for any call to 'images' and 'AbstractController' in my project but no clue.
What does this error mean and what does the AbstractController do?
Txs in advance
The error means you are missing a method images in your ClientsController? Or at least: tried to access that.
The AbstractController is a class inside Rails which serves as a base for every controller. Your ApplicationController derives from it.

Memcached problem with Rails 3. Object isn't being deserialized the second time around

I'm having a problem using memcached in Rails 3
The following is in my controller
#last_post = Rails.cache.fetch('last') {Post.last}
From the view I call #last_post.title
The first time the view is loaded, the title of the last post is displayed. Once the view is refreshed, I get the error undefined method 'title' for #<String:0x8007ae0>
It seems like the object isn't being deserialized the second time around.
Am I doing something wrong? What can I do to fix this? (Ruby 1.8.7, Rails 3.0.1)
Thanks!
I think its to do with Marshal.load, basically rails magically loads all the classes for you but when you call Rails.cache.fetch at some point it will call Marshal.load which doesn’t know anything about the Rails dependency loading and can sometimes silently fail(undefined class/module)
My solution is to simply add
require_dependency 'post'
to your controller which should load the class for the Marshal library to see
I found the solution! Make sure you have your store set in your development.rb regardless if you have caching on or not. Aka add this to development.rb
config.cache_store = :dalli_store

Resources