Best way to conditionally redirect?

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?

+6
redirect ruby ruby-on-rails model-view-controller
source share
3 answers

I think using before_filter in an editing action is the least intrusive.

The reviewer should be reliable enough ... it just has a default value if there is no link (say: someone added a bookmark to the editing page), and everything should be in order.

+1
source share

Flash Data that you will only use for the next request can be stored in flash memory. Then you do not have to clean it automatically. It works great for limited bits of application context, which you will definitely need only once - not just for error messages!

I know that you were making the last HTTP access . If you just need to redirect people to the last URL that they were at, just do it. request.referer, for most browsers that do not block information, gives you the last URL that the person was at.

#in edit controller ... flash[:page_to_redirect_to] = request.referer || "/my/default/path" ... #in save controller redirect_to flash[:page_to_redirect_to] || "/my/default/path" 

I would not suggest hard-coding them, by the way.

before_filter . I see that many Rails developers use this as their favorite hammer and turn everything else into nails. Filters are useful when you want to reveal functionality to all or almost all controller methods. I do not know what is required here, but your mileage may vary. You can combine the filter with the two above tricks, if justified.

+2
source share

Another approach is to load the form into the overlay, for example http://flowplayer.org/tools/overlay/index.html , and then in ajax submit you can close the overlay. I do this in conjunction with autocomplete to offer the option to "add new". After submitting the overlay form, I return some data to json, for which autofill is completed. (I made my own plugin to help with this)

It takes a bit more work, but it really works well for the end user.

0
source share

All Articles