1

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
GeekedOut
  • 16,905
  • 37
  • 107
  • 185
  • 1
    Running `rake routes` will show you how your routes go. Verify it leads where you expect. – Jonathan Allard May 31 '12 at 13:57
  • @jonallard ok, trying it now...my rake is apparently having version problem issues...resolving those now :) – GeekedOut May 31 '12 at 13:59
  • Psssst... I don't know what your situation is, but `bundle install` + [this here](http://stackoverflow.com/a/7553276/720164) (in case) might help – Jonathan Allard May 31 '12 at 14:12
  • @jonallard I just removed the duplicate rake gem, and rank rake routes, but I got this error: rake aborted! undefined method `field' for Kpi:Class – GeekedOut May 31 '12 at 14:13
  • `Kpi:Class` points to your model. It looks like you included a method `field` in there. Can you verify that syntax? Fields are usually contained on the database/schema side. – Jonathan Allard May 31 '12 at 14:24
  • @jonallard I just posted my model. – GeekedOut May 31 '12 at 14:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11990/discussion-between-jonallard-and-geekedout) – Jonathan Allard May 31 '12 at 14:38

1 Answers1

4

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.

  1. Inspect your routes by running rake routes. It's a very helpful tool. It should give you something like this:

        users GET    /users(.:format)          users#index
              POST   /users(.:format)          users#create
     new_user GET    /users/new(.:format)      users#new
    edit_user GET    /users/:id/edit(.:format) users#edit
    

    You can see that it gives you the mapping of what [method, URL] request will hit which [controller, action]. For example, here, POST /users will trigger action create of UsersController.

  2. 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 for UsersController in file app/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.

Community
  • 1
  • 1
Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75