简体   繁体   English

版本化 Web 服务的 Rails 路由

[英]Rails Routes for Versioned Web Services

I'm new to Rails.我是 Rails 的新手。 Can anyone tell me if the following is possible, and if you, what would the correct "route" entry be?谁能告诉我以下是否可能,如果您,正确的“路线”条目是什么?

I have a controller ItemController with one action (index).我有一个 controller ItemController 一个动作(索引)。 When I set it up by default, it's accessible at http://my.app:3000/item , but I want this controller (ie this controller only, not the other controllers) to be accessible via http://my.app:3000/api/v1/ . When I set it up by default, it's accessible at http://my.app:3000/item , but I want this controller (ie this controller only, not the other controllers) to be accessible via http://my.app :3000/api/v1/

Thanks x谢谢 x

Vanessa凡妮莎

Set it up under a namespace:在命名空间下设置它:

namespace :api do
  namespace :v1 do
    resources :items
  end
end

Then you would need to move this controller into app/controllers/api/v1/items_controller.rb and call the class defined in it Api::V1::ItemsController .然后您需要将此 controller 移动到app/controllers/api/v1/items_controller.rb并调用其中定义的Api::V1::ItemsController

Chances are that you're going to h ave some custom logic you want for API actions, so you may want to put that logic in another controller other than ApplicationController .您可能会拥有一些您想要用于 API 操作的自定义逻辑,因此您可能希望将该逻辑放在ApplicationController以外的另一个 controller 中。 This means this new controller would inherit from Api::V1::BaseController rather than ApplicationController .这意味着这个新的 controller 将继承自Api::V1::BaseController而不是ApplicationController

On a side note, I cover this kind of thing in the "Designing an API" chapter of my book, Rails 3 in Action .顺便说一句,我在我的书Rails 3 in Action的“设计 API”一章中介绍了这种事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM