2012-04-05 39 views
1

我目前正在爲我的網站編寫API文檔。API文檔的Rails路線

我不確定寫出路線的「最佳」方法。我覺得微博做了很好的工作,我想複製他們的URL結構:

https://dev.twitter.com/docs/api 
https://dev.twitter.com/docs/api/1/get/statuses/show/:id 
https://dev.twitter.com/docs/api/1/post/statuses/retweet/:id 

這將是這樣的:

namespace :docs do 
    resources :api do 
     # and then... not sure 
    end 
end 

不知道如何寫這部分路線: /get/statuses/show/:id

我應該創建一個自定義路線嗎?

match "/:verb/:resource/:action/:params" => "api#resource" 

或者還有更好的方法嗎?


我最終什麼了,可能幫助別人:)

Ibarcraft::Application.routes.draw do 
    def api_versions; [:v1] end 
    def api_verbs; [ :index, :show ] end 

    constraints subdomain: "api" do 
     scope module: "api", as: "api" do 
      versions = api_versions 

      versions.each do |version| 
       namespace version, defaults: { format: "json" } do 

        # all my routes 
        resources :barcrafts, only: api_verbs do 
         collection do 
          get :search 
         end 
         scope module: "barcraft" do 
          resources :users, only: [:index] 
         end 
        end 
        # and more... 

       end 
      end 

      match 'v:api/*path', to: redirect { |params, request| "/#{versions.last}/#{params[:path]}" + (params[:format] ? ".#{params[:format]}" : "") } 
      match '*path', to: redirect { |params, request| "/#{versions.last}/#{params[:path]}" + (params[:format] ? ".#{params[:format]}" : "") } 
     end 
    end 
end 

回答

1

是你需要自定義路由。

但請注意,這:action關鍵是保留給控制器的動作名稱,最好使用像:action_name

+0

我貼我上面的路線,如果有人感興趣。 – Robin 2012-04-16 20:06:11