2016-04-26 7 views
0

Ich versuche, eine einfache JSON API meiner APPRails API - Loaderror in API :: V1 :: # Userscontroller Index

#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 gibt mir folgendes hinzuzufügen:

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 

Als ich http://localhost:3000/api/v1/users besuchen bekam ich folgende Fehlermeldung:

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

Antwort

1

die Fehlermeldung sagt genau das, was zu tun ist. Stellen Sie sicher, dass Sie API anstelle von Api in Ihrem Controller-Namen verwenden.

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

    def index 
    respond_with User.all 
    end 
end 
+0

Danke, dieses Problem behoben mein Problem. Jetzt habe ich andere Fehler, die ich versuchen werde, selbst zu lösen (oder ich werde spezifische Fragen zu SO öffnen). – davideghz

0

Oder zweite Möglichkeit, ein weiteres Akronym erklären, oder entfernen Sie es trotzdem:

inflections.rb:

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