Reputation: 21
I'm building a simple app to pull a json object from open nasa api.
This is my Model:
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
And this is my controller:
class ApodController < ApplicationController
def index
@apod_info = Apod.get_apod_info
end
end
When I try to load the view in the browser, I get the error below at the bottom.
I have a database (I run db:create), but I'm not using it, so I have no schema and no migrations. But it sounds like this error might have something to do with the database?
I tried different things (even creating a migration with the model and running it), but I couldn't fix it. Similar error messages that I found online seem triggered by conditions different than mine.
Any ideas? Thanks!
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)
Upvotes: 0
Views: 738
Reputation: 20263
If Apod
isn't a database-backed model, then try using a plain old ruby object like (and yes, this should be a class method, not an instance method):
class Apod
def self.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
Personally, I like this style:
class Apod
class << self
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
end
But, that's a matter of taste and I know some people don't like it.
Upvotes: 2
Reputation: 882
Change the method definition to
def self.get_apod_info
/* code */
end
You are calling class method so add self in the method declaration
Upvotes: 1