Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Routing - How to use scope to create admin prefix

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)))'
like image 965
jyoseph Avatar asked Jan 03 '11 03:01

jyoseph


2 Answers

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.

like image 96
Sean Hill Avatar answered Nov 17 '22 14:11

Sean Hill


Try this should work

namespace :admin do 

  resources :pages 

end

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

like image 3
sameera207 Avatar answered Nov 17 '22 13:11

sameera207