Rails time select to show only hour?
Asked Answered
F

2

7

How do I make Rails time select to show only the hour part without the minutes? Basically what I want is to show a time filter with start_time and end_time and use Ransack gem to filter the query.

# db
create_table "schedules"
  t.time "start_time"
  t.time "end_time"

# controller
@q = Schedule.ransack(params[:q])
@q.sorts = ['date asc', 'start_time asc', 'end_time asc'] if @q.sorts.empty?
@schedules = @q.result.includes(:klass, :partner, :city)

#view
<%= search_form_for @q do |f| %>
  <%= f.label :start_time_gteq, "Start time" %>
  <%= f.select_hour :start_time_gteq, start_hour: 5, end_hour: 23 %>

  <%= f.submit "Filter" %>
<% end %>

But it gives me this error:

undefined method `select_hour' for #<Ransack::Helpers::FormBuilder:0x007fb85217e000>

I also tried:

<%= f.time_select :start_time_eq, start_hour: 5, end_hour: 23 %>

...but it shows the minute part. I checked with the API but there's no way to show only hours with the time_select.

EDIT:

Apparently there is a way to remove the minute part:

<%= f.time_select :start_time_gteq, discard_minute: true %>

Unfortunately it also generates hidden input tags that defaulted to the system's current minute, which made the filtering useless.

Frankforter answered 23/6, 2015 at 16:43 Comment(2)
What happens when you give {minute_step: 00} with time_select?Hockey
Error: step can't be 0Frankforter
U
2

Maybe it's time to just generate your own custom select rather than trying to force-fit the Rails helpers... perhaps something like this:

<%= f.label "start_hour_eq", "Start time" %>
<%= f.select "start_hour_eq", (5..23).to_a %>:00

Depending on what input Ransack expects you may need to tweak the name and/or include a hidden field for the minutes:

= hidden_field_tag "start_minute_eq", 0
Unshaped answered 23/6, 2015 at 18:19 Comment(4)
So rather than using t.time "start_time" for the database schema I should've used t.integer "start_hour"?Frankforter
It depends, you could probably continue working with the same database schema. Just take a look at the HTML that using Rails' time_select helper generates, and then manually generate form elements with the same name. Remember Ransack doesn't care what view helpers you use, it just cares what HTML is present in the form, and the HTML generated by the Rails helpers is easy to imitate.Unshaped
I tried f.select :start_time_gteq, (5..23) but Ransack uses the date part of the time column to filter the time: ...WHERE ("schedules"."start_time" >= '2015-06-13 00:00:00.000000')Frankforter
Again, start with the time_select helper (which works correctly with Ransack) and look at the HTML form elements that it creates. Then just recreate those using your own select or select_tag helpers. You don't have to worry about troubleshooting Ransack issues on the controller side (which can be frustrating), just focus on imitating the HTML.Unshaped
R
0

How about: https://apidock.com/rails/ActionView/Helpers/DateHelper/select_hour

select_hour(datetime, options = {}, html_options = {}) public

Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.

Room answered 10/12, 2018 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.