Intelligent Redirects: redirect_to_last_index

I often came across the same problem, the user starts say /users?order=email%20desc, chooses the user to edit and when he hits save/destroy he finds himself at /users/, very disturbing…. Another example comes from multiple controllers, when you start in /movies/3 and then, edit the user that created the movie, hit save and you resurface in /users/.

Fixing this redirecting issue was easier then i thought, and i refactored it to be a nice drop in and forget solution!

  after_filter :save_last_index , :only => :index

  def redirect_to_last_index
    return redirect_to session[:last_index] if session[:last_index]
    redirect_to :action => :index

  def save_last_index

  def destroy
    @user = User.find(params[:id]).destroy

No more redirecting pain hurray 😀

Live example

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s