- 我尝试了三种组合:
namespace :notifications do
put :clear
end
scope :notifications, controller: :notifications do
put :clear
end
resources :notifications, only: [] do
collection do
put :clear
end
end
rails routes:
notifications_clear PUT /notifications/clear(.:format) notifications#clear
clear PUT /notifications/clear(.:format) notifications#clear
clear_notifications PUT /notifications/clear(.:format) notifications#clear # I choose this
由于 url helper clear_notifications_*
,我将选择第三种组合。
- 此外,实际上我想将这些路由嵌套在资源中:
resources :users do
namespace :notifications do
put :clear
end
scope :notifications, controller: :notifications do
put :clear
end
resources :notifications, only: [] do
collection do
put :clear
end
end
end
rails routes:
user_notifications_clear PUT /users/:user_id/notifications/clear(.:format) notifications/users#clear
user_clear PUT /notifications/users/:user_id/clear(.:format) notifications#clear
clear_user_notifications PUT /users/:user_id/notifications/clear(.:format) notifications#clear
最好使用resources
block with only: []
。
PS我认为通过在用户下命名通知控制器更有意义:
resources :users, module: :users do
resources :notifications, only: [] do
collection do
put :clear
end
end
end
clear_user_notifications PUT /users/:user_id/notifications/clear(.:format) users/notifications#clear