I have an application that allows an admin user to switch into another user’s context. If the admin is in /images and switches to another user’s context, the admin will see the Images belonging to that user. I have a ContextController that switches the context, and then, upon success, redirects the user to :back.
This is great, and works well. The problem is that, if the admin is viewing /images/1, and switches into another user’s context, an error will be thrown (because Image with id=1 does not belong to that user).
One way I could handle this is to catch the error, and redirect the user to root_path. I would like a more intelligent way to handle this, however, if anybody has any ideas. Is there a nice programmatic way to check where :back points to, and extract the :action from that path?
Thanks,
Max
:backis just using the http referer header which you can look at inrequest.refererIt will be a full URL, so it won’t be as easy as checking the action, you’ll need to match against the full string url. Make sure to handle the case whenrequest.refereris nil or empty as well.