我有一个简单的Rails应用程序和Grape API Endpoint。我跟着这个tutorial,所以我设置了所有内容,只为用户替换了hussars。我有麻烦使这个工作,因为我的路线似乎已损坏。我有以下文件夹结构:
├── app
│ ├── controllers
│ │ ├── api
│ │ │ ├── base.rb
│ │ │ └── v2
│ │ │ ├── api.rb
│ │ │ ├── defaults.rb
│ │ │ └── users.rb
base.rb
module API
class Base < Grape::API
mount API::V2::Api
end
end
api.rb
module API
module V2
class Api < Grape::API
mount API::V2::Users
end
end
end
users.rb的
module API
module V2
class Users < Grape::API
include API::V2::Defaults
group :tests do
get "/test_1" do
{ test: "all"}
end
end
get "/hello" do
User.all.to_json
end
get '/rt_count' do
{ rt_count: "current_user.rt_count" }
end
end
end
end
并在我的 Defaults.rb 中,我有以下代码:
module API
module V2
module Defaults
extend ActiveSupport::Concern
included do
version 'v2', using: :path
default_format :json
format :json
content_type :json, 'application/json'
#formatter :json, Grape::Formatter::ActiveModelSerializers
rescue_from Mongoid::Errors::DocumentNotFound do |e|
error_response(message: e.message, status: 404)
end
end
end
end
end
的routes.rb
Rails.application.routes.draw do
mount API::Base => '/api'
列出路线(使用gem 'grape-rails-routes'
):
GET /:version/tests/test_1(.:format) API::V2::Users
GET /:version/hello(.:format) API::V2::Users
GET /:version/rt_count(.:format) API::V2::Users
OPTIONS /:version/tests/test_1(.:format)(.:format) API::V2::Users
PUT /:version/tests/test_1(.:format)(.:format) API::V2::Users
POST /:version/tests/test_1(.:format)(.:format) API::V2::Users
DELETE /:version/tests/test_1(.:format)(.:format) API::V2::Users
PATCH /:version/tests/test_1(.:format)(.:format) API::V2::Users
OPTIONS /:version/hello(.:format)(.:format) API::V2::Users
PUT /:version/hello(.:format)(.:format) API::V2::Users
POST /:version/hello(.:format)(.:format) API::V2::Users
DELETE /:version/hello(.:format)(.:format) API::V2::Users
PATCH /:version/hello(.:format)(.:format) API::V2::Users
OPTIONS /:version/rt_count(.:format)(.:format) API::V2::Users
PUT /:version/rt_count(.:format)(.:format) API::V2::Users
POST /:version/rt_count(.:format)(.:format) API::V2::Users
DELETE /:version/rt_count(.:format)(.:format) API::V2::Users
PATCH /:version/rt_count(.:format)(.:format) API::V2::Users
GET /:version/tests/test_1(.:format) API::V2::Api
GET /:version/hello(.:format) API::V2::Api
GET /:version/rt_count(.:format) API::V2::Api
OPTIONS /:version/tests/test_1(.:format)(.:format) API::V2::Api
PUT /:version/tests/test_1(.:format)(.:format) API::V2::Api
POST /:version/tests/test_1(.:format)(.:format) API::V2::Api
DELETE /:version/tests/test_1(.:format)(.:format) API::V2::Api
PATCH /:version/tests/test_1(.:format)(.:format) API::V2::Api
OPTIONS /:version/hello(.:format)(.:format) API::V2::Api
PUT /:version/hello(.:format)(.:format) API::V2::Api
POST /:version/hello(.:format)(.:format) API::V2::Api
DELETE /:version/hello(.:format)(.:format) API::V2::Api
PATCH /:version/hello(.:format)(.:format) API::V2::Api
OPTIONS /:version/rt_count(.:format)(.:format) API::V2::Api
PUT /:version/rt_count(.:format)(.:format) API::V2::Api
POST /:version/rt_count(.:format)(.:format) API::V2::Api
DELETE /:version/rt_count(.:format)(.:format) API::V2::Api
PATCH /:version/rt_count(.:format)(.:format) API::V2::Api
GET /:version/tests/test_1(.:format) API::Base
GET /:version/hello(.:format) API::Base
GET /:version/rt_count(.:format) API::Base
首先,我对这些路线感到困惑,这些路线是从Grape开箱即用的。我几乎没有问题:
(.:format)(.:format)
现在有趣的东西: 当我打电话请求时:
我非常肯定,Grape命名空间中有一些小设置或配置,我无法找到。
答案 0 :(得分:0)
我认为这是因为group
是参数定义块,而不是路径块,将其更改为namespace
。
答案 1 :(得分:0)
使用此代码替换users.rb
module API
module V2
class Users < Grape::API
include API::V2::Defaults
resource :tests do
get "/test_1" do
{ test: "all"}
end
get "/hello" do
{ User.all.to_json }
end
get '/rt_count' do
{ rt_count: "current_user.rt_count" }
end
end
end
end
end