调用ActionMailer时的NoMethodError .deliver_later

时间:2017-06-06 14:31:24

标签: ruby-on-rails actionmailer ruby-on-rails-5.1

当尝试使用Rails5.1调用.deliver_later来测试ActionMailer的功能时,我遇到了一个似乎无法找到该方法的错误。我能够制作一个“测试应用程序”,我可以通过演练使其工作,但将其集成到我当前的项目中会不断破坏。

我比较两个项目之间唯一的主要区别是脚手架是用在一个有效的,而在我试图将它整合到其中的那个不是。以下是有问题的应用程序的内容。

微量

app/mailers/application_mailer.rb:1:in `<top (required)>'
app/mailers/mailer.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:26:in `login'
This error occurred while loading the following files:
   action_mailer/base

MyProject的/应用/控制器/ users_controller.rb

class UsersController < ApplicationController
    def login
        @user = User.find_by('email = ?', params[:user][:email])

        if @user && @user.authenticate(params[:user][:password])
            ##email tester
            UserMailer.welcome_email(@user).deliver_later
            session[:user_id] = @user.id
            session[:user_email] = @user.email
            redirect_to '/'
        else
            flash[:login] = ["Invalid Email or Password"]
            redirect_back(fallback_location: users_signin_path)
        end
    end
end

MyProject的/应用/视图/ user_mailer文件/ welcome_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' 
/>
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up to example.com,
      your username is: <%= @user.login %>.<br>
    </p>

    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

MyProject的/应用/邮寄者/ application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: 'from@example.com'
  layout 'mailer'
end

MyProject的/应用/邮寄者/ user_mailer.rb

class UserMailer < ApplicationMailer
  default from: 'notifications@example.com'

  def welcome_email(user)
    @user = user
    @url  = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to MyApp!')
  end
end

的Gemfile

gem 'rails', '~> 5.1.1'
gem 'pg', '~> 0.20'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'bcrypt', '~> 3.1.7'
gem 'geocoder'
gem 'geokit-rails'
gem 'carrierwave'
gem 'delayed_job_active_record'
gem 'filterrific'


group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'hirb'
  gem 'rails-footnotes'
end

应用/配置/环境/ development.rb

Rails.application.configure do
  config.cache_classes = false
  config.eager_load = false
  config.consider_all_requests_local = true
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true
    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}"
    }
  else
    config.action_controller.perform_caching = false
    config.cache_store = :null_store
  end
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.perform_caching = false
  config.action_mailer.perform_deliveries = true
  config.action_mailer.deliver_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'example.com',
    user_name:            'myemail@gmail.com',
    password:             'my_pw_for_gmail_acct',
    authentication:       'plain',
    enable_starttls_auto: true  }
  config.active_support.deprecation = :log
  config.active_record.migration_error = :page_load
  config.assets.debug = true
  config.assets.quiet = true
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

0 个答案:

没有答案