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