7

This is how a common namespace looks like.

namespace :admin do
  resources :posts
end

And it creates a named route like this one;

new_admin_post_path

Here's my question; how can I add a prefix (like "new" in this example) to a named route under namespace?

Let's say my route definition likes this one;

namespace :admin do
  get 'post/new' => 'posts#new', as: 'post'
end

And it's creates a named route like;

admin_post_path

I want to add "new" prefix to this named route and make it look like new_admin_post_path and I don't want to use resources.

Eren CAY
  • 686
  • 1
  • 7
  • 17

1 Answers1

10

Just try the code in routes.

namespace :admin, as: '' do
   get '/post/new' => 'posts#new', as: 'new_admin_post'
end

If you don't want to make admin namespace as nil, then you can do it. for that you need to put that route out of the namespace :admin block in routes

namespace :admin do
   # your other routes
end

get '/admin/post/new' => 'admin/posts#new', :as => 'new_admin_post'
Bachan Smruty
  • 5,686
  • 1
  • 18
  • 23
  • There are other routes mapped under `admin` namespace so there is no point to drop "admin" prefixes and omit every single one manually. If it's possible I want to able to add prefixes to a named routes under namespaces, like `resources` able to do. – Eren CAY Jun 27 '13 at 13:40
  • 1
    I don't like the idea of forced to getting the route out of the namespace but I think I'll go with this solution for now. I checked quite a bit of options out and read some source code but it is pretty much time consuming for a problem like this one. – Eren CAY Jun 27 '13 at 14:10
  • Yes, the idea is not as per the rails structure. if you will find some better solution for this in between, please post it here. – Bachan Smruty Jun 27 '13 at 14:15