ruby-on-rails

Rails find current route namespace


My routes:

namespace :admin do
  resources :plans
end
resources :plans

Give me the following paths:

localhost:3000/admin/posts
localhost:3000/posts

In my views I want to have something like:

- if request.path.admin?
- if current_namespace.admin?
  puts "Admin"
- else
  puts "User"

=> How can I validate if a current route namespace is /admin?


Solution

  • You should be able to add additional params to your namespace, like:

    namespace :admin, admin: true do
      # ...
    end
    

    Then within that you'll get params[:admin] being true.

    This might require defaults: { admin: true } to work, it's not clear from the docs.