Silly Question that I can't answer


#1

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 :frowning:

Cheers
Mohit.


#2

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 :frowning:


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


#3

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.


#4

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

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

Cheers
Mohit.


#5

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.


#6

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