# Silly Question that I can't answer

**URL:** <https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567>\
**Category:** Rails\
**Created:** [December 28, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567 "2006-12-28T18:23:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [December 28, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567/1 "2006-12-28T18:23:02Z")

</div>

I have a scaffolded page that shows me all the records in a table. Now,  
I added a simple FIND to allow you to filter the list. The text box  
form code is something like this:

Road Name:

and the ‘find’ action is as follows:  
def find  
if request.post?  
qa=[]  
qv=[]  
if params[:name]  
qa \<\< [‘name like ?’]  
str = ‘%’ + params[:name] + ‘%’  
qv \<\< str  
end  
@condition = [qa.join(’ and ')] + qv  
@road\_pages, @roads = paginate(:roads,  
:per\_page =\> 20,  
:conditions =\> @condition,  
:order\_by =\> @sort\_order)  
end  
render :action =\> ‘list’  
end

All this works fine and the first page shows up fine! The problem is  
with the pagination. When you click on the ‘next’ page, naturally it  
loses the search conditions. How do I make them stick? I know there’s  
a simple answer but it’s eluding me ☹

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [December 28, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567/2 "2006-12-28T18:51:09Z")

</div>

On Fri, 2006-12-29 at 01:20 +0800, Mohit S. wrote:

> def find  
> :per\_page =\> 20,  
> :conditions =\> @condition,  
> :order\_by =\> @sort\_order)  
> end  
> render :action =\> ‘list’  
> end
> 
> All this works fine and the first page shows up fine! The problem is  
> with the pagination. When you click on the ‘next’ page, naturally it  
> loses the search conditions. How do I make them stick? I know there’s  
> a simple answer but it’s eluding me ☹

* * *

wish it were that simple.

You have to pass the params with the ‘next page’ link.

I have gotten to the point where I put the ‘search params’ and ‘sort  
params’ into session variables and each time I invoke the list view, I  
deduce/set the session variables so I don’t have to track them through  
passed params from each next|previous link

Craig

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [December 28, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567/3 "2006-12-28T18:55:12Z")

</div>

Craig W. wrote:

> > ```
> > @condition = [qa.join(' and ')] + qv
> > 
> > ```
> > 
> > loses the search conditions. How do I make them stick? I know
> 
> Craig

Thanks Craig,

I am now doing something like that. Since ‘name’ is my only condition,  
I added the following line to the controller’s find action storing the  
“name” as an instance variable so that I can access it in the view.  
@s\_name = params[:name]

Also, I’m now using find.rhtml as the template instead of rendering the  
list.rhtml as I had hoped.

Then, I added this to the find.rhtml for the pagination:  
\<%= link\_to ‘Previous page’, { :page =\> @road\_pages.current.previous,  
:name =\> @s\_name }, :post =\> true if @road\_pages.current.previous %\>  
\<%= link\_to ‘Next page’, { :page =\> @road\_pages.current.next, :name =\>  
@s\_name}, :post =\> true if @road\_pages.current.next %\>

This works, but it requires me to have a different view page for the  
search (as against reusing the list.rhtml). Also, it’s perfectly fine  
for 1 search parameter, but it gets more complicated if there are a  
number of parameters…

Any ideas?  
Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [December 29, 2006, 4:56am UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567/4 "2006-12-29T04:56:54Z")

</div>

Justin F. wrote:

> > This works, but it requires me to have a different view page for the  
> > def list  
> > end  
> > :params =\> {:filter =\> @params[:filter]}) %\>
> 
> * * *
> ...
> 
> Now I wanted to use GET in the above code to make the first page  
> bookmarkable, but it didn’t work on first try and I didn’t spend any  
> time investigating it. If you find out how, I’d be interested to know.
> 
> regards
> 
> Justin F.  
> Hi Justin,

On my ‘list’ page, I have changed the form to be as follows:

Road Name:

This allows me to use GET for the request and gives my pages the right  
kind of URL:  
[http://localhost:4000/roads/find?name=adam](http://localhost:4000/roads/find?name=adam)

I’m not sure if this is the correct way, though.

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [December 28, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567/5 "2006-12-28T23:45:58Z")

</div>

Mohit S. wrote:

> > > qv \<\< str  
> > > All this works fine and the first page shows up fine! The problem is  
> > > deduce/set the session variables so I don’t have to track them through
> 
> This works, but it requires me to have a different view page for the  
> search (as against reusing the list.rhtml). Also, it’s perfectly fine  
> for 1 search parameter, but it gets more complicated if there are a  
> number of parameters…
> 
> Any ideas?

Here are some snippets from some code I wrote many months ago -

in the controller:

def list  
filter = params[:filter]  
conds = nil  
if filter && !filter.blank?  
conds = [‘name like ?’, filter + ‘%’]  
end

```
 @paginator, @items = paginate :pages,
                               :per_page => 30,
                               :conditions => conds,
                               :order => 'name ASC'

```

end

and in the view:

…

\<%= form\_tag :action =\> :list %\>  
Filter: \<%= text\_field\_tag :filter, @params[:filter] %\>  
  
\<%= end\_form\_tag %\>  
Page: \<%= pagination\_links(@paginator,  
:params =\> {:filter =\> @params[:filter]}) %\>

* * *
...

Now I wanted to use GET in the above code to make the first page  
bookmarkable, but it didn’t work on first try and I didn’t spend any  
time investigating it. If you find out how, I’d be interested to know.

regards

Justin F.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [December 29, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/silly-question-that-i-cant-answer/80567/6 "2006-12-29T12:53:41Z")

</div>

Mohit S. wrote:

> I’m not sure if this is the correct way, though.

That’s fine - the problem I was having was with the Rails form helpers.

regards

Justin
