rake how to fix undefined local variable or method error?

♀尐吖头ヾ 提交于 2020-01-25 19:16:46

问题


i have a rake task as follows-

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

Yesterday this task working, but I don't know why it is not working today. I am trying to execute this with the below command-

rake send_fetch_request

Class details:

  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

I am getting below error:

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

Yesterday it was working fine, I don't why rake is complaining today. Please help.


回答1:


Please, look carefully at the error message:

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

The first giveaway is that you get a NameError for an undefined local variable or method, and not for a constant. The second giveaway is the name that Ruby complains about: it doesn't complain about FacebookCrawl, it complains about   FacebookCrawl.

In Ruby, only the Unicode characters U+0020 SPACE and U+0009 CHARACTER TABULATION are treated as whitespace. You have two Chinese U+3000 IDEOGRAPHIC SPACE characters, which Ruby simply treats as part of the name, and since they are not uppercase characters, it treats the name as a local variable or message send.

You should probably turn on visible whitespace in your editor, e.g. this is how your code looks in my editor, making it immediately obvious where the problem is:




回答2:


Copy the facebook_crawl.rb in app/services folder & restart the server. This is your business logic, so it should be in services folder & not in the models So create a services folder as well in the app directory.



来源:https://stackoverflow.com/questions/42409664/rake-how-to-fix-undefined-local-variable-or-method-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!