# How to pass sorting values between pages

**URL:** <https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387>\
**Category:** Rails\
**Created:** [May 12, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387 "2006-05-12T23:31:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [May 12, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387/1 "2006-05-12T23:31:57Z")

</div>

I have several values that need to get passed on every link on a page  
that lists items. For example, as the user paginates, I need to keep  
the current sort value, how many items to display on the page,  
whether to show active or inactive items in the list, etc

In addition, I want them passed through edit actions as well. The  
user edits a record, gets sent back to the list, it should be sorted  
correctly, etc.

Currently I’m placing the relevant param values on every link, and  
it’s just feeling cumbersome. I don’t want to place them in the  
session, because then you can’t have two windows open looking at the  
same list in different ways.

Any recommendations on the best method of doing this?

Thanks,  
Brett

---

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [May 12, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387/2 "2006-05-12T23:53:20Z")

</div>

> you can’t have two windows open looking at the same list in different ways.
> 
> Any recommendations on the best method of doing this?

Seems like sessions are perfect for this, but if your users open  
multiple  
windows to the same data, then your right you have problems.

Not sure it’s better, but you could generate a “window id” and pass that  
around in your links, then store the rest in a session keyed by “window  
id”…

?

-philip

---

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [May 13, 2006, 12:02am UTC](https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387/3 "2006-05-13T00:02:42Z")

</div>

You might consider writing a helper that re-implements link\_to or  
something and tag on the extra options there.

Chris

---

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [May 13, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387/4 "2006-05-13T15:54:20Z")

</div>

Thanks guys. I will have to look at the windows\_id method and maybe  
writing a link\_to helper. Though there might be something out there  
already.

Cheers,  
Brett

---

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [May 13, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/how-to-pass-sorting-values-between-pages/60387/5 "2006-05-13T17:22:51Z")

</div>

On Saturday, May 13, 2006, at 8:50 AM, Brett W. wrote:

> > something and tag on the extra options there.  
> > To: removed\_email\_address@domain.invalid
> > 
> > Rails mailing list  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > I did something like this a while back… the basic approach I ended up  
> > using was to create a view\_settings table and then I stored the YAML of  
> > the params hash with the page\_uri and the user\_id.

The table looked like this, I think

id, :integer  
uri, :string  
user\_id, :integer  
params, :text

You can then use a before\_filter in your controller to recover the  
params hash, overwrite any params with values that were actually passed  
with the request, and then render the page.

You can use an after\_filter to update or create the appropriate entry  
for the view. I would limit this before\_filter to :only=\>:list or other  
actions that actually have settings to save, otherwise the table will  
get huge and will end up with a lot of dead view\_settings for deleted  
records and such.

The advantgage of this method is that it transparently remembers the  
state that each user left the page in last time they were there, and  
this setting persists across sessions.

You could hack this approach a bit and have it remember settings for two  
separate views to the same page. You would only need to find a  
consistent way to make the ‘uri’ key different.

You could generate a unique window id (perhaps based on the current  
time) and pass it in the params if one doesn’t already exist. This  
could be added to the URI to make it a unique entry.

You would need some method to clean out old view\_settings that don’t  
apply anymore. If you use the unique window approach, you also won’t be  
able to persist settings without a bit more work.

\_Kevin
