26

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.

3 Answers 3

27

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
4
  • 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? Jul 19, 2013 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 stackoverflow.com/q/17465335/1297435
    – rails_id
    Jul 19, 2013 at 12:52
  • 12
    Yeah, I feel like the :only => [] is the most explicit and less hacky solution. Thanks Jul 19, 2013 at 14:00
  • I agree with @AugustinRiedinger, using only: [] looks way more cleaner for me, then you can use member and collection as usual Mar 31, 2020 at 16:17
2
  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
1
post "notifications/clear" => "notifications#clear"
2
  • 2
    Please consider explaining why your answer is correct, and what it means to do. Jul 19, 2013 at 10:47
  • I want to group all the notification actions and to have a method :action in it, like resources does, but without the default actions that I don't use. Jul 19, 2013 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.