【问题标题】:nomethod error in ActiveJobActiveJob 中的 nomethod 错误
【发布时间】:2017-10-14 12:19:43
【问题描述】:

运行bundle exec rake jobs:work后出现以下错误

NoMethodError 失败(0 次尝试):未定义方法 `post_it' for # Class:0x00000003375e80>

class PostCreationJob < ActiveJob::Base
  queue_as :default

  def perform(user_id, string, url)
    Message.post_it(user_id, string, url)
  end
end

class Message < ActiveRecord::Base

  def post_it(id, string, url)
    scraper = ForumScraper.new
    scraper.tpt_login(id)
    scraper.make_post(string, url) 
  end      
end

class MessagesController < ApplicationController

   def create
     @message = Message.new(message_params)
     @message.save
     PostCreationJob.perform_later(@message.user_id, @message.string, @message.url)
     redirect_to :back
     flash[:info] = "Posts are being processed."
   end
end

我正在使用 Rails 4.2.5

application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)

module Workspace
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.active_job.queue_adapter = :delayed_job
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby delayed-job rails-activejob


    【解决方案1】:

    post_it 方法被定义为实例方法,但是当您调用Message.post_it 时,您将其作为类方法调用,因此在类中找不到该方法并引发无方法异常。如果您将post_it 定义为def self.post_it,它将可以作为类方法从类中访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多