25

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
  • 17,418
  • 8
  • 58
  • 76
Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206

3 Answers3

25

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
  • 11,297
  • 19
  • 82
  • 107
rails_id
  • 8,120
  • 4
  • 46
  • 84
  • 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
  • 11
    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
  • 485
  • 2
  • 5
1
  1. I tried three combinations:
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

Because of the url helper clear_notifications_*, I will choose the 3rd combination.


  1. Moreover, actually I want to nest these routes in a resource:
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

Still better to use resources block with only: [].


P.S. I think it makes more sense by namespacing the notifications controller under users:

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
hungmi
  • 335
  • 7
  • 11