Time in Rails mode allows you to display only an hour?

How do I set up Rails time to show only the hour part without minutes? Basically, I want to show a time filter with start_time and end_time and use a Ransack gem to filter the request.

 # 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 this 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 he shows the minute part. I checked with the API , but there is no way to show only hours with time_select .

EDIT:

There seems to be a way to remove the minute part:

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

Unfortunately, it also generates hidden input tags, which by default do not match the current minute, which made filtering useless.

+5
source share
2 answers

Maybe it's time to just create your own custom choice, rather than trying to force Rails helpers to force ... maybe something like this:

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

Depending on what Ransack expects, you may need to configure the name and / or enable the hidden field for minutes:

 = hidden_field_tag "start_minute_eq", 0 
+2
source

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

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

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

0
source

All Articles