Rails routing: resources with only custom actions

Augustin Riedinger picture Augustin Riedinger · Jul 19, 2013 · Viewed 12.9k times · Source

I have a NotificationsController, in which I only have the action clear.

I'd like to access this action by doing POST /notifications/clear

So I wrote this in my router:

  resources :notifications, :only => [] do
    collection do
      post :clear
    end
  end

Is there a cleaner way to achieve this? I thought

  scope :notifications do
    post :clear
  end

would do it, but I have a missing controller error, because - I think - it looks for the clear controller.

Answer

rails_id picture rails_id · Jul 19, 2013

If you are using scope, you should add a controller looks like

scope :notifications, :controller => 'notifications' do
  post 'clear'
end

Or just use namespace

namespace :notifications do
  post 'clear'
end