質問

So I'm trying to make a mailer for my site that sends all of the users an email when an order is created. I'm not exactly sure how to go about it, so I've been trying a bunch of different things. Currently, I've screwed up so badly that I can't even get my app to start. (NOT GOOD.)

This is my error message: /Users/Brawain/rails_projects/sample_app/config/initializers/setup_mail.rb:12:in `': uninitialized constant DevelopmentMailInterceptor (NameError)

Currently, this is what my mailer looks like:

Orders controller

class OrdersController < ApplicationController
def new

  def create
    @order = Order.new(user_params)
    if @order.save
        UserMailer.work_order(@user).deliver
    else
      render 'new'
    end
  end

end
end

A new file I made called development_mail_interceptor.rb which is found in the config folder in the environments folder.

class DevelopmentMailInterceptor
def self.delivering_email(message)
message.subject = "#{message.to} #{message.subject}"
message.to = "cwolford@andover.edu"
end
end

The file work_order.text.rb in the views for the user_mailer just has text in it.

In the config folder in the initializers folder I have a file titled setup_mail.rb

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => "student.andover.edu",
  :user_name            => "techmasterswork",
  :password             => "happycrazy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?

And then under app I have a folder called mailers where I have the file user_mailer.rb

class UserMailer < ActionMailer::Base
default from: "techmasterswork@gmail.com"

def work_order(user)
  @user = user
  mail to: user.email, subject: "Work Order Form"
end
end

I have a sneaking suspicion it should actually end up being order_mailer.rb, but I'm not sure. Anyway, I know this needs a lot of help. How would you fix this?

役に立ちましたか?

解決

Can you try putting the DevelopmentMailInterceptor class in the lib directory as development_mail_interceptor.rb, then in your setup_mail.rb file, include the line require 'development_mail_interceptor'

他のヒント

The error is occurring because that class is not loaded up into the rails environment (probably not part of the standard load path). If you go to your rails console and just type the class name DevelopmentMailInterceptor, you'll get the same error. You can try requiring the file in your setup_mail.rb.

BUT, really you should re-think your approach on this. A more standard (and simpler) way of doing this would be to trigger the email on an after_create create callback from you Order class. Just call UserMailer.work_order(user).deliver in the callback.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top