NoMethodError表不存在

NoMethodError表不存在

我正在构建一个简单的应用程序,从开放的nasa api中提取json对象。
这是我的模型:

class Apod < ApplicationRecord
  def get_apod_info
    apod_url = "https://api.nasa.gov/planetary/apod?api_key=#{ENV['NASA_API_KEY']}"
    response = Net::HTTP.get(URI(apod_url))
    JSON.parse(response)
  end
end

这是我的控制器:
class ApodController < ApplicationController
  def index
    @apod_info = Apod.get_apod_info
  end
end

当我试图在浏览器中加载视图时,在底部会出现下面的错误。
我有一个数据库(我运行db:create),但我没有使用它,所以我没有模式,也没有迁移。但听起来这个错误可能和数据库有关?
我尝试了不同的方法(甚至用模型创建一个迁移并运行它),但是我无法修复它。我在网上发现的类似错误信息似乎是由不同于我的情况触发的。
有什么想法吗?谢谢!
Processing by ApodController#index as HTML
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.0ms)


NoMethodError (undefined method `get_apod_info' for Apod(Table doesn't exist):Class):

app/controllers/apod_controller.rb:3:in `index'
  Rendering /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.1ms)
  Rendering /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.4ms)
  Rendering /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /Users/aaa/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)

最佳答案

将方法定义更改为

def self.get_apod_info
   /* code */
end

您正在调用类方法,因此在方法声明中添加self

关于ruby-on-rails - 在禁用PostGres的Rails 5中,“NoMethodError表不存在”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42409074/

10-10 11:54