Gem 用 grape 出现坑了,访问不了!

stephen · 2014年06月10日 · 最后由 fresh_fish 回复于 2014年06月10日 · 3032 次阅读

rails 4 + grape 0.7.1 按照官方的 guide 做,只是改了改一些命名! 试过

http://localhost:3000/api/nodes
http://localhost:3000/api/nodes.json

但是怎么都访问不了 API,出现错误No route matches [GET] "/api/nodes" 请教下,哪里出现错误了? 以下是代码

app
   /api
      /mywy
         api.rb
         entities.rb
         helper.rb

api.rb

require "entities"
require "helpers"

module Mywy
  class API < Grape::API
    prefix "api"
    version "v1"
    format :json

    resource :nodes do
      desc "Return list of nodes"
      get do
        @nodes = Node.all
        present @nodes, with: APIEntities::Topic
      end
    end
  end
end

route.rb

Mywy::Application.routes.draw do
  mount Mywy::API => '/'
  ...
end

application.rb

config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]

http://localhost:3000/api/v1/nodes.json

同楼上,如果不想在 url 里出现 version 字符串,可以放到 header 里:

version 'v1', using: :header
需要 登录 后方可回复, 如果你还没有账号请 注册新账号