Rails 3 Routing - How to use scope to create admin prefix
Asked Answered
D

3

8

I'm using this Rails Guide to create a scope in order to create an "/admin" prefix for some controllers.

So I have a controller named Pages, I want to access it via "/admin/pages".

scope "/admin" do
    resources :pages
end

That works great, but it is still accessible via "/pages" ... How do I prevent that? (I'm using Rails 3)

Here's my routes file:

devise_for :users

scope "/admin" do

    resources :pages

    resources :contents

end

root :to => "index#index"   

match ':controller(/:action(/:id(.:format)))'
Danicadanice answered 3/1, 2011 at 3:29 Comment(0)
B
7

Your syntax for the namespace is correct, but you need to remove the catch-all match from the last line because, according to the default routes.rb file,

# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.

If the requested URL does not match the namespace you have declared, it will still match against the catch-all route at the end.

Bryonbryony answered 3/1, 2011 at 3:35 Comment(4)
Thanks for the quick answer. Yeah I sure did. I'm a rails newb so I am probably missing something simple. The routes work, but the old routes still work which is what I don't want.Danicadanice
Hmm... I just tried it, so it should work properly as-is. Have you restarted the web server? In development mode, you shouldn't have to restart, but it's worth a shot. Can you post your routes.rb file?Bryonbryony
Updated post with routes file. Thanks for the hand! (Also, I have raked routes and restarted the server)Danicadanice
I updated the answer to take into account your routes.rb file.Bryonbryony
W
3

Try this should work

namespace :admin do 

  resources :pages 

end

http://edgeguides.rubyonrails.org/routing.html

Wentworth answered 3/1, 2011 at 10:13 Comment(1)
This will work, too. However, a namespace is different from a scope. The helper names in the admin namespace would be prefixed with admin_.Bryonbryony
K
0

Try this:

scope "/admin", as: :admin do
  resources :pages
end
Kulun answered 23/8, 2016 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.