Silly Question that I can't answer

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

Cheers Mohit.

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

Craig White wrote:

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

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.

Mohit Sindhwani wrote:

Craig White wrote:

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

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?

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

Justin Forder wrote:

Mohit Sindhwani wrote:

**snip** 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?

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

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.

Mohit Sindhwani wrote:

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.

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

regards

   Justin