Set date format in ruby ​​model (sinatra / datamapper)

I have a ruby ​​model that contains a date attribute that I would like to pass as a parameter in the format dd / MM / yyyy.

However, my sqlite3 db stores data in the format yyyy-MM-dd, so when a date such as 10/20/2010 is transferred, it will not be read in the database.

I use the Sinatra framework and use haml to create markup.

Do I need to write a helper that takes a date string and converts it to the correct format for db? Or can I set the format type in the models attribute?

Thank.

+3
source share
2 answers

; DataMapper . , Date.

http://ruby-doc.org/core/classes/Date.html , Date. :

require 'date'
mydate = '20/10/2010'
mydate_obj = Date::strptime(mydate, '%d/%m/%Y')
puts "#{mydate_obj}" # prints 2010-10-20
+2

, , : Rack::Utils.escape_html(params[:datetime]) html , , "25/02/2013", DataMapper : 16/02/2013 ( html-), ( , - ). , "escape_html" ?

0

Source: https://habr.com/ru/post/1748302/


All Articles