I am trying to make a POST request to /api/kpi?data=some+stuff:
curl -i http://127.0.0.1:9010/api/create_kpi -F data="some stuff"
but I’m getting a 404.
My routes are:
# config/routes.rb
namespace :api do
resource :kpi, :except => [:edit, :destroy]
end
Which should hit my controller
# app/controllers/api/kpi_controller.rb
class Api::KpiController < ApplicationController
def create
temp = Kpi.new(params[:data])
end
end
So I am guessing the paths are not correct. Right? I am having a hard time understanding whether my route is incorrect, or the controller, or the call.
When you get a 404, check your routes. It usually means there is no route to the controller to reach. Routes are what makes the link between URLs and controllers. If your controller was getting hit, it’d either work or give you a runtime error.
Inspect your routes by running
rake routes. It’s a very helpful tool. It should give you something like this:You can see that it gives you the mapping of what
[method, URL]request will hit which[controller, action]. For example, here,POST /userswill trigger actioncreateofUsersController.Given a controller/resource name, Rails will, by convention, go looking for the plural of that name. For example, given
resources :user, Rails will go looking forUsersControllerin fileapp/controllers/users_controller.rb. (Path/file names have to match the name!)@yfedblum talks about the use of singular and plural in Rails into more detail.