Mike Wiesenhart
Mike Wiesenhart

Reputation: 316

Rails 4 Mailer Uninitialized Constant

I am trying to setup a mailer to where when a listing's available date is today, it will fire off a mailer. To do that, I am using Date.today. The other relevant code and error is below. Thanks in advance.

availabke_date_mailer.rb

class ListingAvailableDateMailer < ActionMailer::Base
default from: "Nooklyn <[email protected]>"

  def listing_available_expire(listing, agent)
    @listing = listing
    @agent = agent
    mail to: "#{agent.email}", subject: 'Availability of your listing needs to be changed!'
  end
end

listing_available_expire_notification.html.erb:

Hiya <%= @agent.first_name %>,<br><br>

The Available Date for your listing has passed. Please make the necessary changes.<br><br>

Listing: <%= link_to @listing.short_address, @listing, target: "_blank" %><br><br>
Available Date: <%= @listing.date_available %><br><br>`

available_date.rake:

namespace :listings do
  desc "Send a message to an agent if the available date on their listing has passed"
  task listing_available_expire: :environment do
    Listing.all.each do |listing|
      if listing.date_available == Date.today   
        ListingAvailableDateMailer.listing_available_expire(listing,listing.listing_agent).deliver_now
      end
    end
  end
end

Error: enter image description here

Upvotes: 2

Views: 4184

Answers (2)

stevec
stevec

Reputation: 52268

Another reason for this error is if you try to run something like

UserMailer.signup_confirmation(@user).deliver
NameError: uninitialized constant UserMailer

from the rails console.

Note that the exact same code will work from the controller.

A simple way to test such code is to simply place it in a controller action that doesn't require any effort to execute, for example, the users show view is a good candidate, so simply loading that page will cause an email to send

Upvotes: 0

spickermann
spickermann

Reputation: 106812

Rename your availabke_date_mailer.rb and store it at the correct location:

app/mailers/listing_available_date_mailer.rb

Rails does a lot of magic for you, but the magic only works if you follow its conventions. One of the conventions that allows autoloading and removes the need to require all files manually is that class names (in camelcase) have to match the name of the file (in underscore) in which they are defined.

In a next step, you will have to rename your view. Because following Rails conventions it needs to be named like this:

app/views/listing_available_date_mailer/listing_available_expire.html.erb

Read about the Action Mailer Basics in the Rails Guides.

Upvotes: 6

Related Questions