Rails:为资源添加命名空间

Mat*_*Mat 3 routes ruby-on-rails

我目前在我的routes.rb

namespace :api
  namespace :v1
   namespace :me
    # ...
    resources :offers do
     resources :state, only: %i(index)
    end
  end
 end
end
Run Code Online (Sandbox Code Playgroud)

这给了我这条路线:

GET v1/me/offers/:offer_id/state(.:format)
api/v1/me/state#index
Run Code Online (Sandbox Code Playgroud)

但我想要的路线是这样的:

GET v1/me/offers/:offer_id/state(.:format)
api/v1/me/offers/state#index
Run Code Online (Sandbox Code Playgroud)

简单地说,我希望能够将我的state_controller.rb放在一个offers文件夹中,而无需更改访问它的路径。我怎样才能做到这一点?

Mat*_*Mat 5

我找到了一个更好的方法:使用 module

resources :offers, module: :offers do
  resources :state, only: %i(index)
end
Run Code Online (Sandbox Code Playgroud)