I have created a rails project that has some code that I would like to execute as an API. I am using the rails-api gem.
The file is located in app/controllers/api/stats.rb.
I would like to be able to execute that script and return json output by visiting a link such as this - http://sampleapi.com/stats/?location=USA?state=Florida.
How should I configure my project so that when I visit that link it runs my code?
Here's how I would think of this:
in app/controllers/api/stats_controller.rb
in config/routes.rb
Let me know if this works
the file should be called
stats_controller.rb
app/controllers/api/stats_controller.rb
you can create an
index
method where you can add your codein the file
config/routes.rb
you should addTo access the params in the url you can do it in your index method with
params[:location]
,params[:state]