简体   繁体   中英

Rails API - LoadError in API::V1::UsersController#index

I'm trying to add a simple JSON API to my APP

#routes.rb
namespace :api, :format => :json do
  namespace :v1 do
    resources :users
  end
end


#controllers/api/v1/users_controller.rb
class Api::V1::UsersController < ApplicationController
  respond_to :json

  def index
    respond_with User.all
  end
end


#inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

rake routes gives me the following:

api_v1_users        GET      /api/v1/users(.:format)             api/v1/users#index
                    POST     /api/v1/users(.:format)             api/v1/users#create
new_api_v1_user     GET      /api/v1/users/new(.:format)         api/v1/users#new
edit_api_v1_user    GET      /api/v1/users/:id/edit(.:format)    api/v1/users#edit
api_v1_user         GET      /api/v1/users/:id(.:format)         api/v1/users#show
                    PATCH    /api/v1/users/:id(.:format)         api/v1/users#update
                    PUT      /api/v1/users/:id(.:format)         api/v1/users#update
                    DELETE   /api/v1/users/:id(.:format)         api/v1/users#destroy

When I visit http://localhost:3000/api/v1/users I got the following error:

LoadError in API::V1::UsersController#index Unable to autoload constant API::V1::UsersController, expected /path/to/app/controllers/api/v1/users_controller.rb to define it

The error message says exactly what to do. Make sure you use API instead of Api in your controller name.

#  == HERE ==
class API::V1::UsersController < ApplicationController
  respond_to :json

  def index
    respond_with User.all
  end
end

Or second way declare another acronym, or remove it anyway:

inflections.rb :

ActiveSupport::Inflector.inflections(:en) do |inflect|
   inflect.acronym 'Api'
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM