【问题标题】:Grape Routing Error - No route matches for action for namespace + explanation葡萄路由错误 - 命名空间的操作没有路由匹配 + 解释
【发布时间】:2014-12-09 10:41:48
【问题描述】:

我有一个带有 Grape API Endpoint 的简单 Rails 应用程序。我关注了这个tutorial,所以我设置了所有内容,只是为用户更换了轻骑兵。我很难使它正常工作,因为我的路线似乎已损坏。我有以下文件夹结构:

├── 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

现在有趣的是: 当我调用请求时:

  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 不工作,返回 No route matches [GET] "/api/v2/tests/test_1.json"

我很确定,在 Grape 命名空间中有一些小的设置或配置,我找不到。

【问题讨论】:

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


    【解决方案1】:

    我想这是因为group是参数定义块,不是路由块,改成namespace吧。

    【讨论】:

    【解决方案2】:

    用此代码替换 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
    

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      相关资源
      最近更新 更多