Delayed_job什么都不做:TypeError:无法将nil转换为String

时间:2013-07-31 09:04:49

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 rubygems delayed-job

关注“https://github.com/collectiveidea/delayed_job”中的delayed_job。我可以知道为什么我的delayed_job什么都不做?我跑了“rake jobs:work”广告得到了这些错误

错误:

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

完成:

  1. 将“gem'delayly_job_active_record'”添加到gemfile
  2. 在控制台中:“rails generate delayed_job:active_record”
  3. 在控制台中:“rake db:migrate”
  4. 我遵循了这条指令:

    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. 在模型
    2. 的顶部添加了“require'dundod_job'”
    3. 重启我的服务器

1 个答案:

答案 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