Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the namespace of a rails controller object?

What is the best way to get the current controller object's namespace? From inspecting self in a current controller action, I see there is a parameter "REQUEST_URI" that contains a string like "foo_namespace/bar", but I was wondering if there is a better way to get it than to get that (that request_uri wasn't even accessible directly from the controller instance object, I think it was nested in some other params)?

like image 659
patrick Avatar asked Dec 13 '22 12:12

patrick


2 Answers

In both the controller and the views, you can parse controller_path, eg.:

namespace = controller_path.split('/').first
like image 196
KenB Avatar answered Dec 28 '22 02:12

KenB


I believe you are able to do params[:controller].split("/").first. This will return that namespace.

like image 37
Ryan Bigg Avatar answered Dec 28 '22 04:12

Ryan Bigg