0

I'm trying to use the guidance here to convert my date format in my model. The reason I am doing this is I want my user to see the date format as 7/20/2014, but I need to save that date into the database as 2014-7-20. I tried to implement that with this code, but it is not changing the date format when creating or reading:

class Inquiry < ActiveRecord::Base
    belongs_to :user

    def date_requested
        read_attribute(:date_requested).strftime("%m/%d/%Y")
    end

    def date_requested=(date_requested)
        write_attribute(:date_requested, date_requested.strftime("%Y-%m-%d"))
    end
end
Community
  • 1
  • 1
Casey
  • 2,611
  • 6
  • 34
  • 60

1 Answers1

0

Ok, I figured this out. Here's what I did to use American style dates with Bootstrap datepicker:

  1. Add the gem american_date to your gemfile: gem 'american_date'. This gem parses a date as m/d/yy into the format required by the database.
  2. Run bundle install in terminal
  3. Set your form field to show a value rather than what is actually in the database:

      <%= f.text_field :requested_date, :value => @inquiry.requested_date.strftime("%m/%d/%Y"), 'data-behaviour' => 'datepicker' %>
    
  4. Edit your index or show file to display the American format: <%= @inquiry.requested_date.strftime("%m/%d/%Y") %>

This is working great for me.

Casey
  • 2,611
  • 6
  • 34
  • 60