# Go back to previous action

**URL:** <https://www.ruby-forum.com/t/go-back-to-previous-action/100774>\
**Category:** Rails\
**Created:** [February 3, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774 "2006-02-03T21:30:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 3, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/1 "2006-02-03T21:30:34Z")

</div>

Hello all!  
I have two views (list\_1 and list\_2) that show a list of users in two  
different ways. In the views for these actions there is a link to edit a  
specific user properties (edit\_user). That works fine, except that when  
I change the user I want to go back to the view that originated the  
request and not to the default list (list\_1).

Can anyone help me?

Best regards,  
Hugo

---

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 3, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/2 "2006-02-03T21:37:25Z")

</div>

I have a loop in which I cycle though a list of timesheet entries,  
somewhat like:

@timesheet.each do |timesheet|  
…  
end

I would like to have “Up” and “Down” links in each row to reorder the  
list. However, this is only allowed to happen for two entries with  
the same date. So if there was only one entry for a particular date,  
there wouldn’t be any links. It’s easy enough to get the previous  
line’s date using an instance variable, but is there any easy way to  
“read ahead” and get the date for the next record? I had to resort to  
using javascript to populate the links when my site was based in PHP.

Thanks!!

---

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 3, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/3 "2006-02-03T22:30:02Z")

</div>

Hugo wrote:

> Hugo

I use two methods. One to drop a ‘cookie crumb’ in the session and one  
to redirect to the cookie crumb. In the edit\_user action, save the  
referrer in the ‘cookie crumb’. In the update redirect to the saved  
cookie crumb.

So in usage its as simple as:  
def edit\_user  
drop\_cookie\_crumb  
… do edit stuff  
end

def update\_user  
… do update stuff  
return\_to\_cookie\_crumb  
end

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 4, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/4 "2006-02-04T17:35:07Z")

</div>

On 2/3/06, Dylan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> there wouldn’t be any links. It’s easy enough to get the previous  
> line’s date using an instance variable, but is there any easy way to  
> “read ahead” and get the date for the next record? I had to resort to  
> using javascript to populate the links when my site was based in PHP.
> 
> Thanks!!
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

each\_with\_index is your friend. Or, alternatively, you can add your  
own iterator.

- Rowan Rodrik

---

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 5, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/5 "2006-02-05T14:45:32Z")

</div>

Hugo wrote:

> Hello Jack. Thanks for your help.  
> I just have two questions:  
> IF a user is using a browser with tabs (like Firefox) and opens list\_1  
> in one tab and list\_2 in another and then clicks edit\_user link in both  
> tabs what will happen? How can I distinguish the two requests so I can  
> send the user to the correct list action?

Try using ‘redirect\_to :back’  
I think this uses the browser window history.

\_Kevin

---

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 6, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/6 "2006-02-06T15:11:01Z")

</div>

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![Hugo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@Hugo](https://www.ruby-forum.com/u/Hugo)\
**Post date:** [February 5, 2006, 12:23pm UTC](https://www.ruby-forum.com/t/go-back-to-previous-action/100774/7 "2006-02-05T12:23:16Z")

</div>

Hello Jack. Thanks for your help.  
I just have two questions:  
IF a user is using a browser with tabs (like Firefox) and opens list\_1  
in one tab and list\_2 in another and then clicks edit\_user link in both  
tabs what will happen? How can I distinguish the two requests so I can  
send the user to the correct list action?

Jack C. wrote:

> Hugo wrote:
> 
> > Hugo
> 
> I use two methods. One to drop a ‘cookie crumb’ in the session and one  
> to redirect to the cookie crumb. In the edit\_user action, save the  
> referrer in the ‘cookie crumb’. In the update redirect to the saved  
> cookie crumb.
> 
> So in usage its as simple as:  
> def edit\_user  
> drop\_cookie\_crumb  
> … do edit stuff  
> end
> 
> def update\_user  
> … do update stuff  
> return\_to\_cookie\_crumb  
> end
> 
> –  
> Jack C.  
> removed\_email\_address@domain.invalid
