Question

I'm trying to implement api versioning, almost the same as I've done here . but i don't seem to get module/folder structure right in rails app, because I get error messages like V1 is not a module /app/api/v1/xml_responses/device.rb:3:in '<module:API>' Directory structure

/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 => '/'

I can't figure out what i'm doing wrong! could you please help me?

Was it helpful?

Solution

I was having similar problems, but then stumbled on this great post that helped me get things to work, and had more complete information than I found elsewhere. See http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails/

Looking at your code, this looks funny:

module XMLResponses::Device
  def self.do_something

Do you mean to do something like this?

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

OTHER TIPS

Make sure that you have this line in application.rb

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

As suggested at grape wiki here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top