views:

140

answers:

2

In the application there is a default report the user see's listing all the calls for a certain phone. However, the user can select a date range to sort the list from. Doing that, everything works correctly, but when the user selects the date range and changes to the second page, the date-range is lost and it goes back to the default view for the second page.

In my controller, I'm checking to see if the date_range param is being passed in. If it isn't, I display the entire listing, if it is, I display the records in between the certain date range.

The problem is, when I click on a new page, the new parameter doesn't include the old date-range that it should.

How do I go about doing this, I was thinking of doing some class level variable test but that isn't working out the way I thought. And I'm pretty stuck.

I don't have the code right in front of me, but if I remember correctly it's something like this:

<% form for :date_range do |f| %>
    <%= f.calendar_date_select :start %>
    <%= f.calendar_date_select :end %>
    <%= f.Submit %>
<% end %>

And in the controller, it's something like:

if params[:date_range] == nil
    find the complete listings without a date range
else
    find the listings that are within the date range
end
A: 

You could modify the link_to (assuming that's how you go through pages) so that it passed the date_range param.

= link_to 'Next', @whatever_path, :date_range => @date_range

where @date_range could be set in your controller by capturing your params in an instance variable.. .

But there may be a better solution.

rbxbx
I'm using a form.submit button.
Ryan
how about adding = hidden_field_tag 'date_range', @date_range within your form.
rbxbx
A: 

The main problem is that you're using a POST request when submitting the form, but will-paginate uses a GET request. You should also use form_tag instead of form_for because form_for will nest the fields in a hash which is not possible with GET.

<% form_tag items_path, :method => 'get' do %>
  <%= calendar_date_select_tag :start_date %>
  <%= calendar_date_select_tag :end_date %>
  <%= submit_tag "Submit", :name => nil %>
<% end %>

Then check params[:start_date] and params[:end_date] directly. You'll need to change items_path to whatever page you want the form to go to.

This is untested but it should get you in the right direction.

ryanb
How do I get it to just go back to the page it was on? Like refresh it self with new data basically.
Ryan
For example, the page it starts on is usage/detail/:id. I'm not sure how to get it to go to the detail path, it works for usage_path but that doesn't help me out much as it just gives me usages/:id with the time parameters and not the detail controller.
Ryan
Untested, but try this: form_tag '', :method => 'get'
ryanb