Search code examples
ruby-on-railsrubyactionmailermailer

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


Solution

  • 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.