我有一个订阅控制器,其路由如下所示:
get 'user/:user_id/subscription_requests' => 'subscriptions#index', as: :subscription_requests
post 'user/:user_id/subscribe' => 'subscriptions#subscribe', as: :user_subscribe
post 'user/:user_id/unsubscribe' => 'subscriptions#unsubscribe', as: :user_unsubscribe
post 'user/:user_id/request_subscription' => 'subscriptions#request_subscription', as: :request_user_subscription
post 'user/:user_id/accept_subscription' => 'subscriptions#accept', as: :accept_subscription_request
post 'user/:user_id/reject_subscription' => 'subscriptions#reject', as: :reject_subscription_request
除了index
之外,所有这些都是非平静的行为。如何使用resources
之类的东西在路径文件中使这个更清洁,同时将user/:user_id/
保留在路径中?
为清晰起见而更新:
我试图避免逐行列出路线,而是做一些像rails提供的宁静动作。类似的东西:
resources :subscription_requests, :only => [:subscribe, :unsubscribe, :reject, :accept, etc]
答案 0 :(得分:7)
您可以使用路由的成员函数。
resources :user, to: 'subscriptions', :only => [] do
member do
get 'subscription_requests'
post 'subscribe'
etc
end
end
这将产生如下的路线:
subscription_requests_user GET /user/:id/subscription_requests(.:format) subscriptions#subscription_requests
subscribe_user POST /user/:id/subscribe(.:format) subscriptions#subscribe
文档:http://guides.rubyonrails.org/routing.html#adding-more-restful-actions