Using Rails v2.1, let's say you have an action for a controller accessible from multiple locations. For example, in a Rails application, you have a link to edit a user from two different views: one from the index view of the users, and the other from another view (say, from the navigation bar on each page).
I am wondering how best to redirect the user to the right place depending on which link they clicked on. For example:
Example 1:
- List all users
- Click "edit" for the user in the list
- The user presses the "save" button in the form, the controller redirects back to 1.
Example 2:
- The user can be on any page of the application, a link to edit the current user is displayed in the navigation panel.
- User clicks a link to edit
- The user clicks βsaveβ on the form, the controller redirects back to any page on which they were when the user clicked the βeditβ link in the navigation panel.
I saw how this was done in the past:
- Placing the parameter on the source edit link with the source controller / action in which the link appeared. To make this drier, you can use @ controller.controller_name and @ controller.action_name in the helper.
- The controller saves the parameters of the session variable.
- After the controller has saved the record, it redirects to the session variable.
What I especially dislike about this solution is the need to add a parameter to each applicable link in the views. I am wondering if there is a way to build all this in the controller.
One of the best ways I was thinking about was this:
- Put the before_filter file in the βmodifyβ action to save the referrer (is it enough enough?) In the session.
- When "update" is hit, the controller is redirected to the session variable, and then deletes the session variable.
Any thoughts on the best way to do this?
redirect ruby ruby-on-rails model-view-controller
Dan harper
source share