0

從「https://github.com/collectiveidea/delayed_job」關注delayed_job。我可否知道爲什麼我的delayed_job什麼都不做?我跑了 「耙作業:工作」 的廣告了這些錯誤Delayed_job什麼都不做:TypeError:無法將nil轉換爲字符串

錯誤:

Company#update_count_without_delay failed with TypeError: can't convert nil into String - 2 failed attempts 

完成:

  1. 增加了 「寶石 'delayed_job_active_record'」 到的Gemfile
  2. 在控制檯:「軌道生成delayed_job:active_record「
  3. In console:」rake db:migrate「

我跟着這個指令:

If a method should always be run in the background, you can call #handle_asynchronously after the method declaration: 

class Device 
    def deliver 
    # long running method 
    end 
    handle_asynchronously :deliver 
end 

device = Device.new 
device.deliver 

型號:

require 'json' 
require 'net/http' 
require 'rubygems' 
require 'delayed_job' 

class Company < ActiveRecord::Base 
before_save :validate_fbid 

scope :toplikes, order("count desc").limit(20) 

attr_accessible :desc, :fbid, :name, :url, :count 

url_regex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ 


validates :name, :presence => true 

validates :url,  :presence => true, 
       :format  => { :with => url_regex }, 
       :uniqueness => { :case_sensitive => false } 
validates :fbid, :presence => true 
validates :desc, :presence => true 

def update_count 
    uri = URI("http://graph.facebook.com/" + fbid) 
    data = Net::HTTP.get(uri) 
    self.count = JSON.parse(data)['likes'] 
end 

handle_asynchronously :update_count  
end 

控制器:

def create 
    company = Company.new(params[:company]) 

    if company.save 
     @message = "New company created." 
     company.update_count 
     redirect_to root_path 
    else 
     @message = "Company create attempt failed. Please try again." 
     redirect_to new_path 
    end    
    end 
  1. 補充說: 「需要 '的delayed_job'」 在模型上
  2. 重啓我的服務器
+0

顯示您的公司類,尤其是實施公司#計數='。 –

+0

@MarekLipka更新了CompanyController,我沒有Company#計數。 –

+0

我的意思是'公司'模型,而不是控制器。 –

回答

1

您的Company#update_count錯誤。它應該是:

def update_count 
    uri = URI("http://graph.facebook.com/" + fbid) 
    data = Net::HTTP.get(uri) 
    # here you should use your local variable that you set in previous line 
    # instead of unset instance variable: 
    update_attribute(:count, JSON.parse(data)['count']) 
end 
+0

謝謝!這是一個愚蠢的錯誤。任務已完成,但我的self.count仍然爲零。 –

+0

@shoujo_sm,因爲您沒有將其保存在'update_count'方法中。我更新了我的答案以反映這一點。 –

+0

@shoujo_sm有幫助嗎? –

相關問題