ruby-on-railsruby-on-rails-3rails-routingrestful-architecture

Rails routing: resources with only custom actions


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.


Solution

  • 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