2

I have been looking for a solution for this problem for hours. There are plenty of recommendations even here on stackoverflow and I tried all of them but none of them work in my case and I don't know why.

I have the current version of RoR [4.1.5] running on my server and I have a form in which the user has to select a "time from" and a "time to" value.

<p>
<%= f.label :Time %><br>
<%= f.time_select :timefrom, {minute_step: 60} %> to <%= f.time_select :timeto, {minute_step: 60} %>
</p>

I get the default values from the database. In order to fetch them, I just collect the last time value which has been entered. Which usually is a timeframe of 1 hour:

<% changetimeto = (Production.select("timeto").last.timeto).strftime("%H:%M")   %>
<% changetimefrom = (Production.select("timeto").last.timeto + 3600).strftime("%H:%M")  %>

So I tried:

<%= f.time_select :timefrom, {minute_step: 60}, :selected => changetimeto %> to <%= f.time_select :timeto, {minute_step: 60}, :selected => changetimefrom %>

or :value => or :default =>

but nothing seems to work, it doesn't even give me an error. I checked the variables in changetimeto and changetimefor and they have the right value, its like "14:00" and "15:00". But as I said, it doesn't do anything.

I also tried to address hours and minutes separately with :default => {hours: '14', minutes: '00'}, etc. but that also doesn't work, not even causing an error. Just nothing. It still defaults to the actual time.

Does anyone have a recommendation or hint for me?

Coenwulf
  • 1,937
  • 2
  • 15
  • 23
RichiMartin
  • 81
  • 2
  • 10
  • Tag helpers will typically have a non-hash field expecting a default value, e.g. select_tag :foo, @widget.foo || params[:foo]. I'll look into the syntax for time_select. – Vardarac Aug 26 '14 at 18:53
  • Sorry, i don't know what you mean by that – RichiMartin Aug 26 '14 at 18:57
  • Sadly doesnt work... Showing C:/i2data_input/app/views/productions/_form.html.erb where line #23 raised: undefined method `merge' for "16:00":String – RichiMartin Aug 26 '14 at 19:08
  • My mistake. I misunderstood what the second argument in time_select does. I thought it was a prefill value, but it's actually a method on the object you're working with, e.g. `f.time_select :foo, :bar` gets a value from `f.bar`. [The answer to this question](http://stackoverflow.com/questions/2664803/how-do-i-set-a-time-in-a-time-select-view-helper) is probably what you're looking for - the `:value` option. – Vardarac Aug 26 '14 at 19:11
  • changed it to `

    <%= f.label :Time %>
    <%= f.time_select :timefrom, {minute_step: 60}, :value => @production.timefrom, :default => changetimefrom %> to <%= f.time_select :timeto, {minute_step: 60}, :value => @production.timeto, :default => changetimeto %>

    ` still not working
    – RichiMartin Aug 26 '14 at 19:17
  • 1
    Renra's answer to his own question on that page works on my end, e.g. `time_select :timeto, {minute_step: 60}, :default => {:hour => '10', :minutes => '30'}`. From the looks of it you may need to set the hours and minutes by manipulating your variable. – Vardarac Aug 26 '14 at 19:22
  • Now I changed the code to: `<%= f.time_select :timefrom, :default => {:hour => '10', :minutes => '30'} %>` it will take the hour for the default, but the minutes are still defaulting to the actual minutes – RichiMartin Aug 26 '14 at 19:25
  • My mistake; that's "minute", not "minutes." – Vardarac Aug 26 '14 at 19:33
  • yes, seems to be working now. Sorry, could have spotted that too! THANK YOU VERY MUCH! Now how can I select your answer? – RichiMartin Aug 26 '14 at 19:46

2 Answers2

6

Defaults in time_select (for those who've not been following the comment thread) work like this:

<%= time_select :name, 'method', {options}, {:default => {:hour => '10', :minute => '30'}} %>

If we want the time select to default to 10:30.

Vardarac
  • 563
  • 2
  • 17
2
f.time_select :start, default: Time.current.change(hour: 9, min: 0)

or

f.time_select :start, default: {hour: 9, min: 0}

ref: https://github.com/rails/rails/blob/94b5cd3a20edadd6f6b8cf0bdf1a4d4919df86cb/actionview/lib/action_view/helpers/tags/date_select.rb#L44

rentalname
  • 61
  • 3