The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[rails] Redirecting problem with grouped controllers

Hello,

I have all my admin related controllers grouped under Admin (for example I have Admin::LoginController and Admin::CategoryController). When someone tries to access a protected controller he's redirected to login page and the page he requested is saved in session like this:

Code:

session[:goto] = request.parameters

Now, that's all fine the problem arises after a successful login when I try to redirect user back to the original page he requested. I basically do something like (I also unset the session var and couple other things, but that's not important here):

Code:

redirect_to session[:goto]

Problem is that user is redirected to /admin/admin/category instead of to /admin/category. This is happening because Login controller is already in /admin and request.parameters hash :controller value has a 'admin/category' - and when redirect_to is called it uses a relative path and redirects user to /admin/admin/category.

The trivial solution for this problem is to change :controller value to an absolute path (i.e. '/admin/category') or to remove 'admin/' altogether, but that seems messy and also I am not quite sure how it would work if Rails app is not deployed in server root.

The best way: Don't use namespaced controllers, use routing rules to add an /admin prefix to your non-namespaced controllers, and use regular :controller/:action/:whatever rules without worry since routes.rb will take care of it for you.

The way you might have to do it now because changing your app will take too long: Set the :controller value to '/admin/category'.