简体   繁体   English

使用非平静操作清理Rails路由

[英]Cleaning up Rails routes with non-restful actions

I have a subscriptions controller with routes that look like this: 我有一个订阅控制器,其路由如下所示:

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

All of them except index are non-restful actions. index之外的所有这些都是非平静的动作。 How do you make this cleaner in the routes file using something like resources while keeping the user/:user_id/ in the path? 如何在路径文件中使用类似resources东西使这个更清洁,同时保持user/:user_id/在路径中?

Update for clarity : 更新清晰度

I'm trying to avoid listing the routes line by line and instead do something like what rails provides with the restful actions. 我试图避免逐行列出路由,而是做一些类似rails提供的其余操作。 Something like: 就像是:

resources :subscription_requests, :only => [:subscribe, :unsubscribe, :reject, :accept, etc]

You can use the member function of routing. 您可以使用路由的成员函数。

resources :user, to: 'subscriptions', :only => [] do
  member do
    get 'subscription_requests'
    post 'subscribe'
    etc
  end
end

And this will produce routes such as: 这将产生如下路线:

subscription_requests_user GET    /user/:id/subscription_requests(.:format)        subscriptions#subscription_requests
subscribe_user POST   /user/:id/subscribe(.:format)                    subscriptions#subscribe

Docs: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions 文档: http//guides.rubyonrails.org/routing.html#adding-more-restful-actions

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

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