I am trying to pass date variable from form to method to export info based on created_at field in database. But when i send date format value from my form I get "bad value for range" error. I think it is because variable date format and format in database doesn't match. Any ideas how to solve that?
Here is my .haml form created with simple form:
= simple_form_for :download, { url: download_exports_path('xlsx') } do |f|
= f.input :date, as: :date, html5: true
= f.button :submit, "Export"
And here is my download method in my controller:
def download
current_time = Time.now
date = params[:date]
@addresses = Address::where(created_at: date..DateTime.now.end_of_day)
respond_to do |format|
format.xlsx {render xlsx: 'download',filename: "addresses.xlsx"}
end
end
Any ideas how to solve that?
The where
clause should start with a dot and also you should probably convert the date params to the correct format with to_datetime
.
def download
current_time = Time.now
date = params[:download][:date].to_datetime
@addresses = Address.where(created_at: date..DateTime.now.end_of_day)
respond_to do |format|
format.xlsx {render xlsx: 'download',filename: "addresses.xlsx"}
end
end