3

我有一个订阅控制器,其路由如下所示:

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]

4

1 回答 1

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

于 2013-08-15T13:40:55.237 回答