I have two columns in my table, start_date and end_date, both of which have the type datetime. I'm trying to use text_field with the jQuery gem bootstrap-datepicker-rails to let the user pick the date from a little popup calendar. The problem is that the date that gets returned is not in the format that Ruby expects, and so you can end up with invalid dates which violate the model validations. For instance, picking 10/7/2015 (i.e., Oct 7 2015) as the start date and 10/30/2015 (Oct 30 2015) returns two errors: 1) End date can't be blank and 2) Start date must be before.
I think this is because it's actually interpreting the second date in a different format, and since there's obviously no 30th month in the year, it fails to parse the date. I can test this hypothesis by starting up the rails console and trying to enter the dates manually. For instance, project = Project.new(number: 7, account_manager: "Test", customer: "Test", start_date: "10/7/2015".to_datetime, end_date: "10/30/2015") returns the following: => #<Project number: 7, name: nil, customer: "Test", created_at: nil, updated_at: nil, start_date: "2015-07-10 00:00:00", end_date: nil, account_manager: "Test">. Note that the end_date field is nil.
I think I see the crux of the problem, but I'm struggling to get this to work. I can see that somehow Rails needs to interpret a date entered as 10/30/2015 as Oct 30/2015 but then transform it into the appropriate format before saving the record in the database. Can anyone help me get past this stumbling block?
Here's the projects model. class Project < ActiveRecord::Base validates :start_date, presence: true validates :end_date, presence: true validates :start_date, date: { before: :end_date } validates :customer, presence: true validates :number, presence: true end
And here's the projects/new view.
<h1>Create New Project</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@project) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :number, 'Project Number' %>
<%= f.text_field :number, class: "form-control" %>
<%= f.label :account_manager %>
<%= f.text_field :account_manager, class: "form-control" %>
<%= f.label :customer %>
<%= f.text_field :customer, class: "form-control" %>
<%= f.text_field :start_date, class:"datepicker", :value=> (@project.start_date && @project.start_date.strftime("%m-%d-%Y")), placeholder:"Start Date" %>
<%= f.text_field :end_date, class:"datepicker", :value => (@project.end_date && @project.end_date.strftime("%m-%d-%Y")), placeholder:"End Date" %>
<br>
<%= f.submit "Create project", class: "btn btn-primary" %>
<% end %>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.datepicker').datepicker({todayHighlight: true, orientation: 'auto top'});
});
</script>
Lastly, I know there are several questions (e.g., "Rails 3.1 text_field form helper and jQuery datepicker date format") about this already out there, but I'm having difficulty getting the answers to work, whether due to my own inexperience or changes in the Rails framework since some of the older questions were answered.