运行bundle exec rake jobs:work
使用NoMethodError失败(0次尝试之前):未定义的方法`post_it' #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
答案 0 :(得分:1)
post_it
方法被定义为实例方法,但是当你调用Message.post_it
时,你将它称为类方法,因此在类上找不到该方法并引发no方法异常。如果您将post_it
定义为def self.post_it
,则可以作为类方法从类中访问它。