Search code examples
ruby-on-railsrubyhamlsimple-form

Ruby on rails. bad value for range


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

Here is where i get error: enter image description here

Any ideas how to solve that?


Solution

  • 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