葡萄路由错误 - 没有路由匹配命名空间+解释的操作

时间:2014-12-09 10:41:49

标签: ruby-on-rails ruby api ruby-on-rails-4 grape

我有一个简单的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开箱即用的。我几乎没有问题:

  1. 为什么Grape会为某些API列出两倍的格式? (.:format)(.:format)
  2. 为什么我有大多数API的两个条目?在我看来,它是由文件api.rb中的代码引起的。
  3. 加分问题:如何在Rails中关闭XML处理。我想只支持JSON
  4. 现在有趣的东西: 当我打电话请求时:

    1. http://lclhost.com:3000/api/v2/hello.json - API正在运作 预期
    2. http://lclhost.com:3000/api/v2/rt_count.json - API是 按预期工作
    3. http://lclhost.com:3000/api/v2/tests/test_1.json - API无效,返回 没有路线匹配[GET]“/api/v2/tests/test_1.json"
    4. 我非常肯定,Grape命名空间中有一些小设置或配置,我无法找到。

2 个答案:

答案 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