Using Rails v2.1, lets say you have an action for a controller that is accessible from more than one location. For example, within the Rails app, you have a link to edit a user from two different views, one on the users index view, and another from another view (lets say from the nav bar on every page).
I'm wondering what is the best way to redirect the user back to the right spot depending on what link they clicked on. For example:
Example 1:
- List all users
- Click "edit" on a user in the list
- User clicks "save" on the form, the controller redirects back to 1.
Example 2:
- The user could be on any page within the application, the nav bar shows a link to edit the current user
- The user clicks on the link to edit
- The user clicks "save" on the form, controller redirects back to whatever page they were on when the user clicked the "edit" link in the nav bar.
I've seen it done in the past by:
- Placing a parameter on the original edit link with the original controller/action in which the link appeared. To make this more DRY, you could use @controller.controller_name and @controller.action_name in a helper.
- The controller saves the parameters to a session variable.
- Once the controller has saved the record, it redirects to the session variable.
What I don't particularly like about this solution is the need to add the parameter to every applicable link in the views. I'm wondering if there's a way to build this all into the controller.
One better way I was thinking was to:
- Place a before_filter on the "edit" action to save the referrer (is this reliable enough?) into the session.
- When "update" is hit, the controller will redirect to the session variable and then delete the session variable.
Any thoughts on the best way to do this?