【问题标题】:Rails Grape api versioning module structureRails Grape api 版本控制模块结构
【发布时间】:2013-12-12 17:24:09
【问题描述】:

我正在尝试实现 api 版本控制,几乎与 I've done here 相同。但我似乎没有在 rails 应用程序中获得正确的模块/文件夹结构,因为我收到类似 V1 is not a module /app/api/v1/xml_responses/device.rb:3:in '<module:API>' 的错误消息 目录结构

/app
  /api
    - api.rb
    /v1
      -base.rb
      /xml_responces
        - device.rb

api.rb

require 'v1/base.rb'
module  API
  class Base < Grape::API
    mount API::V1 => '/v1/'
  end
end

v1/base.rb

module API
  module V1
  class ApiV1 < Grape::API
    require 'builder'
    helpers DeviceMethods
    prefix 'api'
    version 'v1', using: :header
  end
  end
end

V1/xml_responses/device.rb

module API
  module V1
    module XMLResponses::Device
      def self.do_something
        #do_something
      end
    end
  end
end

Routes.rb

  mount API::Base => '/'

我不知道我做错了什么!你能帮帮我吗?

【问题讨论】:

  • 尝试在您的 device.rb 文件顶部要求 v1/base.rb。
  • 不,这没有帮助
  • 还是同样的模块错误?

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


【解决方案1】:

我遇到了类似的问题,但后来偶然发现了这篇很棒的帖子,它帮助我完成了工作,并且比我在其他地方找到的信息更完整。见http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails/

看看你的代码,这看起来很有趣:

module XMLResponses::Device
  def self.do_something

你的意思是这样做吗?

module API
  module V1
    module XMLResponses
      class Device < Grape::API
        resource :device do
          get do { Device.all } # Or whatever
        end
      end
    end
  end
end

【讨论】:

    【解决方案2】:

    确保application.rb中有这一行

    config.paths.add "app/api", glob: "**/*.rb"
    config.autoload_paths += Dir["#{Rails.root}/app/api/*"]
    

    正如葡萄维基here.所建议的那样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 2016-03-21
      • 2011-10-15
      • 2019-01-15
      • 2014-12-23
      • 2017-05-10
      • 2012-05-14
      相关资源
      最近更新 更多