Forum: Ruby on Rails Silly Question that I can't answer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2006-12-28 18:23
(Received via mailing list)
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:
  <form action="/roads/find" method="post">
    <p><label>Road Name:</label> <input type="text_field" name="name"
size="40"></p>
   <input type="submit" value="Find"></p>
  </form>

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.
Aa33613fb12765fe505efea87844a12e?d=identicon&s=25 Craig White (Guest)
on 2006-12-28 18:51
(Received via mailing list)
On Fri, 2006-12-29 at 01:20 +0800, Mohit Sindhwani 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
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2006-12-28 18:55
(Received via mailing list)
Craig White 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.
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-12-28 23:45
(Received via mailing list)
Mohit Sindhwani wrote:
>>>    <input type="submit" value="Find"></p>
>>>           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:

...
<!-- Would prefer to use GET, but it isn't working -->
<%= form_tag :action => :list %>
Filter: <%= text_field_tag :filter, @params[:filter] %>
<input type="submit" value="Go">
<%= end_form_tag %>
Page: <%= pagination_links(@paginator,
                            :params => {:filter => @params[:filter]}) %>
<hr/>
...

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 Forder
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2006-12-29 04:56
(Received via mailing list)
Justin Forder wrote:
>>
>> This works, but it requires me to have a different view page for the
>   def list
>   end
>                            :params => {:filter => @params[:filter]}) %>
> <hr/>
> ...
>
> 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 Forder
Hi Justin,

On my 'list' page, I have changed the form to be as follows:
  <form action="/roads/find" method="get">
    <p><label>Road Name:</label> <input type="text_field" name="name"
size="40"></p>
   <input type="submit" value="Find"></p>
  </form>

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.
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-12-29 12:53
(Received via mailing list)
Mohit Sindhwani 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
This topic is locked and can not be replied to.