Question

I am very new to Rails and am using version 3.2.6. So far I have made a simple practice application that includes a datetime. In my form partial I have

<%= form_for(@item) do |f| %>
  [...snip irrelevant form fields...]
  <div class="field">
    <%= f.label :copyright_date, %><br />
    <%= f.datetime_select :copyright_date, { include_blank: true } %>
  </div>
<% end %>

From the options documentation http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select I was under the impression that the { include_blank: true } portion of the datetime_select options hash would allow me to set empty dates. However, if I have any of the date fields (but not time fields) set to the blank option, the whole thing returns nil. I am dealing with archival data where sometimes the entire date is not known, but only a portion. I need to be able to capture all of the datetime data if it exists but leave any unknown parts blank. I thought that was what include_blank was for, but maybe I am misinterpreting the documentation?

Était-ce utile?

La solution

all include_blank does is put a blank space at the beginning of each dropdown menu so you have the option of leaving it blank. without it, it will just default to values and there will be no option to leave it blank.

To do what you described, you could create a datetime object, and set the values for hour, minute, etc through that, and then save it.

http://corelib.rubyonrails.org/classes/DateTime.html#M000524

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top