form_for with datetime_select - ruby-on-rails

I am using Rails 3.1 and first time using datetime_select. I want to specify the minute select box to have 15 minute intervals rather than 1 minute intervals:
<%= f.datetime_select :start_datetime, :ampm => true, :start_minute => [0,15,30,45] %>
But this doesn't seem to work. The :start_datetime is what the property is called in the Event model. Is it possible that this is a reserved word that is causing problems?

Try passing :minute_step => 15
<%= f.datetime_select :start_datetime, :ampm => true, :minute_step => 15 %>

Related

How to make "AM/PM" appear in the minutes dropdown in Rails' datetime_select method? Rails 4

Is there a way to make the "AM/PM" that is displayed in the Rails datetime_select method (when :ampm => true is active), or even a way to separate it into its own dropdown box? Documentation for the datetime_select method is scarce beyond what I linked and nothing seems to address this issue.
Can it be done? Preferably without the use of any JavaScript?
This is what I get when I state this:
<%= f.datetime_select :when, { :ampm => true, :minute_step => 10 }, :class => "form-control" %>

Ruby on Rails: time_select default value

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?
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.
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

Rails - set date_select in form 18 years ago

Very simple question, but can't seem to work it out!! I'm creating a date of birth form, to ensure that a user can only sign up if they are over 18. I've got a date_select, and am fine setting the start year, but how do I set the end year so that it is 18 years ago? I'm going to use validators in the model as well, but don't want someone to be able to enter the wrong info in the first place.
I've looked all over, but can only find info on how to use time.ago with validators, which I've already sorted, not in the form itself.
Or, alternatively, which might be better - is there a way of having a date_select dropdown for the first two items (day & month), and a text-box for the year? Something along these lines:
<%= f.date_select :date_of_birth, :order => [:day, :month] %><%= f.text_field :date_of_birth.year, :length => 4 %>
This is what I have so far:
<%= f.label :date_of_birth %><br />
<%= f.date_select :date_of_birth, :order => [:day, :month, :year], :start_year => 1900, :end_year => %>
Thanks!!
Try
Time.now.year - 18
or
Date.today.year - 18
Found the answer on this page. Just changed it to -18 instead of +5:
<%= f.date_select :date_of_birth, :order => [:day, :month, :year], :start_year => 1900, :end_year => Time.now.year - 18 %>
You can also do:
18.years.ago.year

Datetime_select without seconds, minutes, or hours in a form_for

The rails scaffolding code generates datetime_select like this:
<div class="field">
<%= f.label :time_end %><br />
<%= f.datetime_select :time_end, :order => [:month, :day] %>
</div>
However I only want to display month and day. The time helper has an option called :disable_minutes and :disable_seconds... however that doesn't work with a form_for helper.
Whats the best way to do this?
Well first off I would use date_select (api doc here) and then do :discard_year => true.
Example:
date_select("article", "written_on", :discard_year => true)
Use the :discard, e.g. :discard_year => true, :discard_hours => true

Create a Year Dropdown Box FormBuilder In Ruby On Rails

I am trying to create a db that has a year attribute but I am having some difficulty. I created the scaffold and tryied to modify the _form.html.erb with this code:
<%= f.date_select :year, :start_year=>2000, :end_year=>Time.now.year %>
Tried to run the rails server and gave me this error:
1 error(s) on assignment of multiparameter attributes
I realized that I only wanted the year and not the day or month. Is there a way to do that? I tried :discard_month=>true but that just hides it, but still storing it.
Thanks in advance
Do something like:
<%= f.date_select :year, :order => [:year], :start_year => 2000, :end_year => Time.now.year, :prompt => {:year => "Select year"} %>
Because your year field is an int and not a datetime:
<%= f.select :year, (2000..Time.now.year).to_a, :include_blank => {:year => "Select year"} %>
Another alternative is select_year
e.g.,
select_year(Date.current, start_year: 2015, end_year: Date.current.year)

Resources