Rails路由:仅包含自定义操作的资源

Aug*_*ger 18 ruby-on-rails rails-routing ruby-on-rails-3 restful-architecture

我有一个NotificationsController,我只有行动clear.

我想通过POST/notifications/clear来访问此操作

所以我在路由器中写了这个:

  resources :notifications, :only => [] do
    collection do
      post :clear
    end
  end
Run Code Online (Sandbox Code Playgroud)

有没有更清洁的方法来实现这一目标?我想

  scope :notifications do
    post :clear
  end
Run Code Online (Sandbox Code Playgroud)

会这样做,但我有一个missing controller错误,因为 - 我认为 - 它寻找clear控制器.

rai*_*_id 20

如果您正在使用范围,则应添加一个控制器

scope :notifications, :controller => 'notifications' do
  post 'clear'
end
Run Code Online (Sandbox Code Playgroud)

或者只使用命名空间

namespace :notifications do
  post 'clear'
end
Run Code Online (Sandbox Code Playgroud)

  • 是的,我觉得`:only => []`是最明确,最不那么狡猾的解决方案.谢谢 (6认同)