Displaying a different date than is submitted rails datetime_field - ruby-on-rails

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?

Related

Formatting Orbeon Date input fields

I am trying to format an input field of the type xs:date in Orbeon.
I have tried using the xxf:format attribute, but the datepicker can not understand the date when it has been modified.
The idea now was to change the javascript of Orbeon to use the xxf:unformat attribute to interpret the date and transform it back to ISO format.
I've tried changing the data.js but for some reason none of the changes can be seen.
Am I changing the wrong file?
Edit
I figured out that the xforms.js has a function 'getCurrentValue' which is being as the changes I do there are visible. Now I just need to figure out who is the one that's calling the function.
Edit:
It is the Calendar who requests the value of the input when the user clicks on the symbol. This all happens at the client side, and the generated HTML does not have the format/unformat attributes. However I want to use their value. Can I make a request to Orbeon to get it? How?
In case you're using an xf:input bound to a node of type xs:date, you can control the formatting of the date field with the oxf.xforms.format.input.date property. A few formats are supported, and if you want to add more, the best would be to follow the pattern currently used for the currently supported formats.
E.g.
[M]/[D]/[Y]
[Y]-[M01]-[D01]

change columns date column format in ruby sqlite3

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.

x-editable with meteor textarea no line breaks and date timezone issue

I'm currently trying to work in bootstrap x-editable to my meteor application. I'm using the atmosphere package for this: https://github.com/nate-strauser/meteor-x-editable-bootstrap. I'm having a couple of issue so far which are:
When I select a date using the date data-type I get a javascript date object back that is 4 hours behind what I actually picked(assuming this is because I'm in -4 timezone).
When I edit a textarea, the line breaks are saved to the database, but when bring up the editable to edit it the line breaks are striped.
It looks like this might be the intended behavior. Its hard to be sure without more information.
With the date, javascript stores date in unixtime. This is because its very easy to switch timezones and not worry about having javascript itself having to keep track of DST and the other complications of keeping time.
If you use new Date(<the javascript timestamp>); you should get the time in your timezone.
With the textarea it looks like some kind of text-encoding conversion is taking place. You should check to see what the character codes of those stripes are and convert them to newlines like \n. One scenario this could occur is if you're copy-pasting stuff with a different encoding into the textarea.

Can someone explain Rails behavior for multiple fields with identical names and ids?

I needed to create a input mask for a jQuery datepicker in my Rails app, where the first form field uses m/d/yy format and the datepicker populates a hidden input with the proper database format.
I was using SimpleForm, and so I extended my own input so that the input is preceded by the mask.
I got everything set up and when checking out the browser, it all just worked well before I thought I would be done.
The form ends up with two inputs for the same attribute, each with the same id and name. I never thought this would work. Checking the development log I only see one date getting submitted, the second of the two which is the one that has the proper format for the database.
Is this all okay? Should I take some extra steps even though this appears to work fine, and more importantly, can someone explain what's going on under the hood that results in this behavior?
Thanks!!
Rails uses the Hash params to store fields submitted. When you declare two or more inputs using the same name, it happens the same as if you do something like
h=Hash.new
h[:name]="foo"
h[:name]="bar"
Result is bar because the foo was overwritten. So the "winner" is always the field value which the browser last appended to postdata.
But if I where you, I would not rely on the browser that the second field gets appended at last.

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.

Resources