环境:rails4.2.1, grape0.11.0
#app/api/v1/root.rb
class V1::Root < Grape::API
version 'v1'
format :json
get :hello do
{hello: "world"}
end
end
#route.rb
mount V1::Root => "/api"
访问http://localhost:3000/api/v1/hello
的结果是正确的{"hello":"world"}
但 rake routes 只能显示这样
Prefix Verb URI Pattern Controller#Action
v1_root /api V1::Root
解决方案:建一个 rake task rails g test grape routes
#lib/tasks/grape.rake
namespace :grape do
desc "TODO"
task routes: :environment do
V1::Root.routes.each do |route|
path = route.route_path
path.sub!("(.:format)",".json")
path.sub!("/:version","")
r = {
method: route.route_method,
path: path,
params: route.route_params,
desc: route.route_description
}
puts r.map{|k,v| "#{k}:#{v}"}.join(' ')
end
end
end
执行 rake grape:routes
结果 method:GET path:/hello params:{} desc: