Rails 3: Get current namespace?

arnekolja picture arnekolja · Nov 19, 2010 · Viewed 23k times · Source

using a method :layout_for_namespace I set my app's layout depending on whether I am in frontend or backend, as the backend is using an namespace "admin".

I could not find a pretty way to find out which namespace I am, the only way I found is by parsing the string from params[:controller]. Of course that's easy, seems to be fail-safe and working good. But I am just wondering if there's a better, prepared, way to do this. Does anyone know?

Currently I am just using the following method:

def is_backend_namespace?
  params[:controller].index("admin/") == 0
end

Thanks in advance

Arne

Answer

William Wong Garay picture William Wong Garay · Feb 22, 2013

You can use:

self.class.parent == Admin