22

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.

Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
Augustin Riedinger
  • 16,966
  • 22
  • 99
  • 173

2 Answers2

22

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
cweston
  • 10,371
  • 17
  • 74
  • 104
rails_id
  • 8,012
  • 4
  • 42
  • 79
  • 1
    And if I need both a collection do and a member do in it? I still need to define the `post '/:id/edit` manually? – Augustin Riedinger Jul 19 '13 at 11:04
  • In your case and use scope or namespace, yes (`get :edit, :path => '/:id/edit'`) . If you don't need manually you could use your wrote on your question. Read here http://stackoverflow.com/q/17465335/1297435 – rails_id Jul 19 '13 at 12:52
  • 10
    Yeah, I feel like the `:only => []` is the most explicit and less hacky solution. Thanks – Augustin Riedinger Jul 19 '13 at 14:00
  • I agree with @AugustinRiedinger, using only: [] looks way more cleaner for me, then you can use member and collection as usual – Amir El-Bashary Mar 31 '20 at 16:17
1
post "notifications/clear" => "notifications#clear"
yxf
  • 427
  • 2
  • 4