# Can will\_paginate and select\_datetime play nice?

**URL:** <https://www.ruby-forum.com/t/can-will-paginate-and-select-datetime-play-nice/118061>\
**Category:** Rails\
**Created:** [October 23, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/can-will-paginate-and-select-datetime-play-nice/118061 "2007-10-23T17:33:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![podo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@podo](https://www.ruby-forum.com/u/podo)\
**Post date:** [October 23, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/can-will-paginate-and-select-datetime-play-nice/118061/1 "2007-10-23T17:33:44Z")

</div>

I have a search form with two select\_datetime helpers, "“start” and  
“end”, which I use to find results between a range of datetime values.  
They return the correct results, but if I click any of the pagination  
links provided by will\_paginate, I get the following error: “You have  
a nil object when you didn’t expect it!” when the controller tries to  
access params[:start\_date]. AFAIK, this is because will\_paginate  
creates a link like:  
“[http://somehost/log/list](http://somehost/log/list)?  
end\_date=month10minute33hour10day23year2007&page=2&start\_date=month9minute33hour10day23year2007&commit=Search”  
Is there a method to turn that into a date object or something I can  
use to run query with?

See [Parked at Loopia](http://pastie.caboo.se/109899) or the following:

\*\*\* ./views/log/list.rhtml \*\*\*  
\<%= select\_datetime @start\_date, {:prefix =\>  
“start\_date”, :datetime\_separator =\> " - ", :time\_separator =\> " : "}  
%\>  
\<%= select\_datetime @end\_date, {:prefix =\>  
“end\_date”, :datetime\_separator =\> " - ", :time\_separator =\> " : "} %\>  
\<%= will\_paginate @logs %\>

\*\*\* ./controllers/log\_controller.rb \*\*\*  
if params[:start\_date].nil?

# If no params are present, set default start date to one month ago

@start\_date\_sql = Time.now.last\_month.strftime(‘%Y-%m-%d %H:%M:%S’)  
@start\_date = Time.now.last\_month  
else  
@start\_date\_sql = params[:start\_date][:year]+“-”+params[:start\_date]  
[:month]+“-”+params[:start\_date][:day]+" “+params[:start\_date][:hour]  
+”:“+params[:start\_date][:minute]+”:00"  
@start\_date = Time.parse(@start\_date\_sql)  
end

if params[:end\_date].nil?

# If no params are present, set default end date to now

@end\_date\_sql = Time.now.strftime(‘%Y-%m-%d %H:%M:%S’)  
@end\_date = Time.now  
else  
@end\_date\_sql = params[:end\_date][:year]+“-”+params[:end\_date]  
[:month]+“-”+params[:end\_date][:day]+" “+params[:end\_date][:hour]  
+”:“+params[:end\_date][:minute]+”:00"  
@end\_date = Time.parse(@end\_date\_sql)  
end

conditions = [“mydate BETWEEN ? AND ?”, @start\_date\_sql,  
@end\_date\_sql]  
@logs = Log.search(conditions, params[:page])

\*\*\* ./models/log.rb \*\*\*  
def self.search(conditions, page)  
paginate :per\_page =\> 20, :page =\> page, :conditions =\>  
conditions, :order =\> “id DESC”  
end
