1
votes

Sorry if this is a dupe - I looked for the answer thoroughly and couldn't find anything that fit. :(

I'm pretty sure the issue is that my table start_date and end_date fields are datetime format, and datepicker is saving as string - but I can't figure out a way to fix that.

Here's my appointments.coffee script:

    `$(document).ready(function() {
       $('.datepicker')
        .datepicker()
        .on('changeDate', function(ev) {
          $(this).datepicker('hide');
       }); 
     });`

My /app/views/appointments/_form.html.erb looks something like this:

      <div id="drop-off">
          <%= f.label :Drop_off %><br>
          <%= f.text_field :start_date, :class => 'datepicker' %>
      </div>
      <div id="pick-up">
          <%= f.label :Pick_up %><br>
          <%= f.text_field :end_date, :class => 'datepicker' %>
      </div>

Note: The data is getting passed to the server, but the dates are not writing to the database. When I view the output in firebug, it looks like something like this:

method
patch appointment[bird_id]
1 appointment[cage_needed]
0 appointment[checkin]
0 appointment[customer_id]
1 appointment[end_date]
08/31/2015 appointment[notes]
appointment[start_date] 08/23/2015 appointment[user_id]
1

On another note: This worked when I first implemented it, but when I fixed the turbolinks issue (adding //= require jquery.turbolinks to application.js), it stopped saving to database (I think).

Ideas? Thanks!

EDIT: Selecting date in datepicker that is before August 12th 2015 will save to database, but anything after that will not.

EDIT 2: It turns out that it will save if the date is the 1st through the 12th of any month. If it is between the 13th and 30th/31st of the month, it won't save.

2

2 Answers

2
votes

Rails will try to convert the string to a date before saving to the database, but for some reason 'mm/dd/yyyy' is not one of the formats that it expects when performing the conversion (see some sparse documentation here). So, if you open the Rails console and try to do something like '8/13/2015'.to_date, you will get an invalid date error, probably because it interprets 13 as the month rather than the day. But, if you have no validations on the model to ensure that the date is present, Rails will happily persist the model to the database with a nil date.

So, the solution seems to be to change the formatting of the date to one Rails won't complain about. Whether it makes more sense to do this on the JavaScript side or somewhere on the Rails side is up to you.

EDIT More detail on the solution:

You could fix this on the front-end by passing in a format option to your initialization of Datepicker in your appointments.coffee file, as documented:

$(document).ready(function() {
   $('.datepicker')
    .datepicker({ format: 'dd/mm/yyyy' })
    .on('changeDate', function(ev) {
      $(this).datepicker('hide');
   });
 });

Note that the date comes before the month. This will format the date string in a way that Rails understands and can convert properly when saving. This is probably the cleanest fix, but will be a problem if that's not the way you want dates to look to your users.

If you do want to keep the mm/dd/yyyy format for your users, you'll have to do some processing of the string value either before the form gets submitted to the server or after Rails has the submitted form data (but before it tries to use the date strings, since, as we've established, they'll be invalid). Either way is going to be kind of hacky. I would probably use a before_action inside the AppointmentsController to modify the params hash so the date strings are in the correct format:

before_action :process_dates, only: [:create, :update]

def process_dates
  [:start_date, :end_date].each do |d|
    # assuming 'mm/dd/yyyy' format
    date = params[:appointment][d]
    date_parts = date.split('/')
    # swap date and month
    date_parts[0], date_parts[1] = date_parts[1], date_parts[0]
    params[:appointment][d] = date_parts.join('/')
  end
end

(I'm a little rusty so that might not work exactly as written)

0
votes

I was able to resolve by adding {format: "dd-mm-yy"} to my JQuery. Still some formmatting issues, though...

So I added .try(:strftime, "%m/%d/%Y") to my view.