change columns date column format in ruby sqlite3 - ruby-on-rails

I am using sqlite3 for my database environment. I have a column named date in ruby which I created using a scaffold like date:date so the type is date and it is also called date. The date is defaulted by ruby to display the date after typing in the _form_html.erb like e.g. "2015-01-18" .But I want it to display the date like 18-01-2015 on the index.html.erb view. SO no matter what I type into the form the format is set to the year first way so if i type 18-01-2015, then it displays it the other way round but if I type in anything random then it displays blank.

You can create a file called date_formats.rb inside config/initializers/.
# config/initializers/date_formats.rb
Date::DATE_FORMATS[:date_month_year] = '%d-%m-%Y'
Now inside the view call
<%= date.to_s(:date_month_year) %>
The advantage of this approach is, you don't need to define your custome date format each and every view. Whenever you need this format, you just use the corresponding symbol of the format. Read #to_s method.
Note: Date#to_s is an alias of Date#to_formatted_s.

Related

Displaying a different date than is submitted rails datetime_field

I have several datetime_field in my application. By default, they are displaying the date as the rails format, as I dictated that for my bootstrap-datepicker:
format: 'YYYY-MM-DD HH:mm:ss'
The issue: I want the date on the views to display in a different format, say:
'MM-DD-YYYY HH:mm:ss'
I've been scratching my head for a while on this one. Every change I make to the view side, be it by Javascript, jQuery, rails methods, helpers, whatever - they all affect the submit data, which then becomes invalid, because Rails expects the first format.
I don't want to change the default date format - we have other aspects of the website that need the database in the normal rails format. I just want to change the display of the date in the datetime_field, without changing the submitted date.
My guess would be it was an option in the datetime_field, but I can't seem to find one:
<%= f.datetime_field(:end_date, {?????})%>
Any suggestions on how to accomplish this?

How do I get a date from a Rails app into a Sencha Touch 2 app without it being an invalid date?

Problem: Get a date inside of a JSONP response that looks like "2012-11-11T04:27:25Z" (which is the default date output from a Rails app) into a date field in a model instance in a Sencha Touch 2 app without it being an Invalid date.
Background: I have a Rails app that is generating JSONP, which I read into a Store in my Sencha Touch 2 app, and the date, as formatted by Rails comes into the Sencha Touch 2 app as an Invalid Date.
To reproduce: To reproduce the problem for this question and duplicate the error, use the Fiddle below. Note: I took the Rails server out of the picture by hard-wiring the data into a store with a date formatted the same way my Rails app has been outputting them. So, in other words, I have reproduced the entire problem locally in the Sencha Touch 2 app by mimicking what my Rails app does server-side.
Fiddle: http://www.senchafiddle.com/#7qQkb. To see the problem, run the Fiddle from a Webkit browser and view the JavaScript console inside the Webkit Developer Tools.
Symptom: What will happen is that the XTemplate that is created by the List component within the app executes code (inside {[]} brackets) to output a date for your inspection in the console. When you inspect the date that is outputted, you will see: __proto__: Invalid Date. The date will still show up just fine in the view, but because it is an invalid date, I cannot format the date or retrieve different parts of the date object.
My question is this: Is there something that I can do to my model or my store, or alternatively to my Rails app, to get the date into a model instance without it being an Invalid Date?
What you see is just the returned value of toString method in the Date prototype
Date.prototype.toString(); // "Invalid Date"
That´s why absolutly all the "dates" say exactly the samething. However, it doesn´t mean date are wrong! In extjs, if you want to format a date field in you model you should use:
{ name: 'timestamp', type: 'date', dateFormat: "Y-m-d H:i" }
But if you only want to format it to display it, you can use the format yo used, one of the named built-in format or just the sameone I used in the example: Y-m-d H:i
I have answered this question through some more research. The default way that Rails represents dates in a JSON / JSONP response is adequate for Sencha Touch 2. The __proto__ property of the date object will always represent as an Invalid Date, but you can still manipulate the date by using Ext.Date.format() in your XTemplate object as follows:
'<p>{[Ext.Date.format(values.timestamp, "F d, Y g:i A")]}</p>'
(where timestamp is the date you wish to format.)
The fact that it shows up as Invalid Date in the console is of no consequence.

Orbeon form builder: Using date fields with initial value "current-date()"

I am using date fields in Orbeon form builder that should be prefilled with the current date (see http://i42.tinypic.com/erdjrb.jpg). When choosing a date by hand in the form, the date format in resulting XML model is set to "2011-07-12". But when not changing the default value of current-date(), then I get "2011-07-12+02:00". Does anybody know why the date format is different when I prefill it with current-date()?
Thank you!
The XPath function fn:current-date() by definition returns the date together with explicit time zone information. I assume orbeon just passes the function call to the XPath engine (Saxon i think). A quick workaround would be to format the result of current-date() using format-date(), for example:
format-date(current-date(), '[Y]-[M01]-[D01]')
Since i don't use Form Builder, i can't tell in detail, but i assume setting the config options how to format xforms:input controls regarding date and time values applies for form builder, too.

Handling different date formats in ROR

I am trying to build a ROR app that allows users to enter date in various formats such as 12/31/11 (month/day/year) or 31/12/11 (day/month/year). In order to interpret date format, I will have a select field from where user can select the format of date. I can use Date.strptime(value, format).to_s() in controller before saving record.
However, I am not sure is controller right place to put this information. Is there a way I can push this to model..say in before save method.
You could just save the data as it is (in the controller), and have another field in the model telling it how to interpret the data. Then, in a before_validation callback, you could try parsing the date according to the given format and writing it to the same field, now as a date. Problems may arise on the way back. Then, to display it in the view again, you could write helpers - but better yet, provide a method in the model (for instance, formatted_date) that will compute and display the date.
However, this requirement sounds strange. Why is the user's responsibility to select a date format? Shouldn't it be based on the user locale?
In any case, I suggest you register your date formats in an initializer, rather than repeting the format strings throughout the application.

Show new lines (\n) of type string when displaying tables in ruby on rails

I have a table in my rails project, which contains a column of type string. One of the entries for this column is "one\ntwo". When I try to display the table, that entry is displayed as "one two". I serialized the entry, so I tried displaying an inspected version as well, which displayed "one\ntwo". Is there any way to display that entry as following?
one
two
s.gsub(/\n/, '<br>') try this, and play with sanitize or maybe html_safe

Resources