Warum schlägt der Versuch, Grape with Rails zu verwenden, mit der "nicht initialisierten konstanten API" fehl?
Ich möchte, dass jemand erklärt, warum dies in Rails (4.1.8) mit Grape (0.10.1) passiert.
so das ist meine API:
app/api/root.rb
:
module API
class Root < Grape::API
prefix 'api'
mount API::V1::Root
end
end
app/api/v1/root.rb
:
module API::V1
class Root < Grape::API
version 'v1'
mount API::V1::Users
end
end
app/api/v1/users.rb
:
module API::V1
class Users < Grape::API
format 'json'
resource :users do
desc "Return list of users"
get '/' do
User.all
end
end
end
end
config/routes.rb
:
Rails.application.routes.draw do
mount API::Root => '/'
end
nd in meinemapplication.rb
Ich fügte hinzu
config.paths.add "app/api", glob: "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"]
und in diesem Fall erhalte ich den Fehler:NameError: uninitialized constant API
aber wenn mein Code so aussieht:
app/api/root.rb
das gleiche wie obe
dannapp/api/v1/root.rb
:
class Root < Grape::API
version 'v1'
mount Users
end
app/api/v1/users.rb
:
class Users < Grape::API
format 'json'
resource :users do
desc "Return list of users"
get '/' do
User.all
end
end
end
config/routes.rb
:
Rails.application.routes.draw do
mount Root => '/'
end
undconfig/application.rb
das gleiche wie obe
Dann funktioniert alles gut.
Meine Frage ist, warum ich keine Module in @ angeben musv1/root.rb
und auch drinnenv1/users
und auch warum brauche ich nichtAPI::Root => '/'
imconfig/routes.rb
?