Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go 'back' 2 levels?

From the list view of my app, I can view a list of records or drill down and edit/update a record. After updating, I want to go directly back to the list view, bypassing a couple of intermediate pages - but I don't simply want to link_to(:action => list) - there's pagination involved. I want to go back to the exact 'list' page I came from. What's the best way? Pass a hidden arg somewhere with the page number? Is there an elegant way to accomplish this?

like image 254
Joe McGuckin Avatar asked Jan 06 '11 01:01

Joe McGuckin


2 Answers

I'm just going to throw this one out there with the disclaimer that there may be security considerations or existing gems.

On your edit action, you could store the previous page in a session. Then in your update action, redirect to it.

class MyController < ApplicationController

  def edit
    session[:prev_url] = request.referer
  end

  def update
    redirect_to session[:prev_url]
  end

end
like image 55
Peter Brown Avatar answered Sep 21 '22 08:09

Peter Brown


As an alternative to use the session, you could carry the referer through the actions using a hidden form field.

class MyController < ApplicationController

  def edit
    @prev_url = request.referer
  end

  def update
    redirect_to params[:prev_url]
  end

end

Form using hidden_field:

f.hidden_field :prev_url, :value => @prev_url

If you do not want to carry along the whole referer url you could also do the same with the page parameter instead and append the parameter to the url in the update action. I would also expect Rails' url helpers to accept parameters.

like image 31
crispy Avatar answered Sep 18 '22 08:09

crispy