0

I have a datetime value which comes from the API in this format: 2015-07-07T17:30:00+00:00. I simply want to split it up between the date and time values at this point. I am not using an Active Record model and I prefer not to use an sql database if I can.

The way I have set up the app means that the value is "stored" like this in my view: @search.dining_date_and_time

I have tried two approaches to solving this problem:

  1. Manually based on this previous stackoverflow question from 2012: Using multiple input fields for one attribute - but the error I get is the attribute is "nil" even though I put a "try"

  2. Using this gem, https://github.com/ccallebs/split_date_time which is a bit more recent and seems to be a more elegant solution, but after closely following the doc, I get this error, saying my Search model is not initalized and there is no method: undefined method dining_date' for #<Search not initialized> This is when instead I put @search.dining_date in the view, which seems to be the equivalent of the doc's example (its not that clear). The doc also says the method will be automatically generated.

    • Do I need to alter my model so I receive the data from the API in another way? ie. not get the variable back as @search.dining_date_and_time from the Search model for any of this to work?

    • Do I need an Active Record model so that before_filter or before_save logic works - so i can (re)concatenate after splitting so the data is sent back to the API in a format it understands. Can I avoid this - it seems a bit of overkill to restructure the whole app and put in a full database just so I can split and join date/time as needed.

Happy to provide further details, code snippets if required.

Community
  • 1
  • 1
neo5_50
  • 466
  • 1
  • 5
  • 19

1 Answers1

0

As I am not using a conventional Rails DB like MySql Lite or Postgresql, I found that the best solution to the problem was by using this jQuery date Format plugin: https://github.com/phstc/jquery-dateFormat to split the date and time values for display when I get the data back from the API.

The Github docs were not too expansive, but once I put the simply put the library file in my Rails javascript assets folder, I just had to write a few lines of jQuery to get the result and format I wanted:

$(function() {

  var rawDateTime = $('#searchDiningDateTime').html();
  // console.log(rawDateTime);

  var cleanDate = $.format.date(rawDateTime, "ddd, dd/MM/yyyy");
  // console.log(cleanDate);
  $('#searchDiningDateTime').html(cleanDate);

  var cleanTime = $.format.date(rawDateTime, "HH:mm");
  // console.log(cleanTime);
  $('#searchTime').html(cleanTime);

});

Next challenge: rejoin the values on submit, so the API can read the data by sending/receiving a valid request/response. (The values can't be split like this when sent to the remote service).

neo5_50
  • 466
  • 1
  • 5
  • 19