2017-02-23 77 views
0

我有一個rake任務如下 -rake如何修復未定義的局部變量或方法錯誤?

desc 'send fetch request' 
    task send_fetch_request: :environment do 
      FacebookCrawl.new.process 
    end 

昨天這個任務的工作,但我不知道爲什麼今天不工作的。 我想用下面的命令 -

rake send_fetch_request 

類的細節執行此:

class FacebookCrawl 

    def initialize     
     fb_config = YAML.load_file(Rails.root.join("config/facebook_catalog.yml")) 
     @access_token = fb_config["facebook"]["access_token"] 
     @product_feed_ids = fb_config["facebook"]["product_feed_ids"] 
    end 

    def process 
     @product_feed_ids.each do |key,value| 
      feed_id = value["id"] 
      feed_url = value["feed_url"] 
      make_request(feed_id,feed_url,@access_token) 
     end 
    end 
end 

我得到以下錯誤:

rake send_fetch_request --trace 
    ** Invoke send_fetch_request (first_time) 
    ** Invoke environment (first_time) 
    ** Execute environment 
    ** Execute send_fetch_request 
    rake aborted! 
    NameError: undefined local variable or method `  FacebookCrawl' for main:Object 
    /Users/raj.sharma/Documents/Developer/Feed/lib/tasks/facebook_fetch_request_task.rake:3:in `block in <top (required)>' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `call' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in execute' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `each' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in invoke_with_call_chain' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `each' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:80:in `block in run' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/bin/rake:22:in `load' 
    /Users/raj.sharma/.rbenv/versions/2.2.2/bin/rake:22:in `<main>' 
    Tasks: TOP => send_fetch_request 

昨天它工作正常,我不是爲什麼耙子今天抱怨。請幫忙。

+0

很明顯,FacebookCrawl是未知的。你是否需要包含類定義的文件? – user1934428

+0

不,我是否需要使用rake中需要的類?如果是的話如何?我是新來的鐵軌。但昨天工作正常。 – RajSharma

+0

FacebookCrawl的位置是什麼? –

回答

4

請在錯誤信息仔細看:

NameError: undefined local variable or method `  FacebookCrawl' for main:Object 
#     ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ ↑↑↑ 

第一個贈品是,你得到一個未定義的局部變量或方法一個NameError,而不是一個常量。第二個贈品是Ruby抱怨的名字:它不抱怨FacebookCrawl,它抱怨  FacebookCrawl

在Ruby,只有Unicode字符U + 0020 SPACEU + 0009 CHARACTER TABULATION被視爲空白。你有兩個中文U + 3000 IDEOGRAPHIC SPACE個字符,Ruby只是簡單地把它當作名字的一部分,而且由於它們不是大寫字符,所以它把名字當作本地變量或消息發送。

您應該在編輯器中打開可見的空格,例如這就是您的代碼在我的編輯器中的外觀,使其立即明顯出現問題:text editor with visible spaces showing the offending characters in different highlighting

+0

謝謝。它在刪除這些空白處後工作。非常感謝。 – RajSharma

+0

你知道如何在Atom編輯器中顯示空白嗎? – RajSharma

+1

那麼,你看到它是**不**代表白色空間。你可以在Jörg用於他的優秀分析的屏幕截圖中看到它:包含FacebookCrawl的行包含6個灰色圓點,代表白色空間,然後在Facebook的「F」之後再沒有點,所以從這裏你可以看到那裏在Ruby之間,這不是一個白色空間。我現在不使用Atom,但如果Atom不會以顯着不同的方式顯示,我會感到驚訝。 – user1934428

-1

facebook_crawl.rb複製到app/services文件夾中&重新啓動服務器。 這是您的業務邏輯,所以它應該在服務文件夾&不在模型中 因此,在應用程序目錄中創建一個服務文件夾。

+0

做完這些之後,我需要將它包含在rake任務中嗎?如果是的話如何? – RajSharma

+0

不,只需複製服務文件夾中的文件並執行您的耙子任務 –

+0

我將其移至app/services/facebook_crawl.rb並重新啓動服務器。同樣的錯誤。 – RajSharma

相關問題