Domanda

I've got a vanilla rails 4 application and I'm trying to add a versioned Grape API. The rails server starts up fine, but when I try to access the API URL (http://localhost:3000/v1/user/ping), I get the error:

undefined method `call' for V1:Module

So far, the API I have setup is very simple, but I can't figure out why it is not working

root/app/api/api.rb:

# root/app/api/api.rb
class API < Grape::API
  mount V1
end

Within the version folder, I want to have all the classes that the version of the API supports: root/app/api/v1/user.rb:

# root/app/api/v1/user.rb
module V1
  class user < Grape::API
    get :ping do
      { :ping => params[:pong] || 'pong' }
    end
  end
end

root/config/routes:

TestApp::Application.routes.draw do
  mount API => '/'
end
È stato utile?

Soluzione

I split my api up into folders:

v1/resources/users.rb
v1/resources/orders.rb
v1/entities/order.rb
v2/resources/orders.rb

and then in api.rb just mount the individual files ...

mount V1::Resources::Users
mount V1::Resources::Orders
mount V2::Resources::Orders

And then:

version ['v2','v1'], cascade: true
version 'v2', cascade: true
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top