bootstrap get crazy date format when editing a date in rails - ruby-on-rails

So, I have a rails application which there is a datepicker. When I put the date for the first time in the calendar it appears in this format: dd-mm-yyyy.
But rails save in the database in this format: yyyy-mm-dd.
When I go to edit this in the form, the datepicker get crazy, it appears a 1900 year, not the actual year.
I know this happens because the format date is different(the date which bootstrap expects and the date which is saved).
I tried change the datepicker format in the edit view(.html.erb) but it doesn't work. How can I solve this?
<%= f.text_field :my_date, "data-provide" => 'datepicker',
'data-date-format' => 'dd-mm-yyyy', class: 'datepicker form-control', :required => true %>

Related

Rails : simple_for_for time with 12 hour format

Is there a proper way to show 12-hour time in Rail's simple_for_for? I am currently using
<%= f.input :time, :ampm => true, :minute_step => 5 %>
however it only displays two drop downs, with the times displayed as 01AM in the first dropdown and 00 in the second.
I would like it to have three drop downs, for hours, minutes, and am/pm.
As per the documentation
HTML 5 date / time inputs are not generated by Simple Form by default, so using date, time or datetime will all generate select boxes using normal Rails helpers. We believe browsers are not totally ready for these yet, but you can easily opt-in on a per-input basis by passing the html5 option:
<%= f.input :time, as: :time, html5: true %>
This should fix your problem.

Format date in ActiveAdmin form

I got a list of trips which I display in ActiveAdmin, the index uses a formats.fr.yml and the date is displayed with the right format.
However, when I modify a trip, in the form, ActiveAdmin doesn't use formats.fr.yml anymore.
Here is the line in the form :
f.input :start_date, as: :date_time_picker
I tried something like this :
f.input :start_date, as: :date_time_picker, :value => :start_date.strftime('%d-%m-%Y %I:%M')
But it doesn't work.
By the way, when I change the date with the datepicker, it changes the date to the right format (but because of some js).
Thanks for your help.
Try this one,
f.input :start_date, :value => :start_date.try(:strftime,'%y-%m-%d %i:%m')
Where try will prevent you from getting exceptions, when date is nil.

stop simple_form from converting datetime to utc

I have a datetime that gets rendered into a hidden form field using simple_form. The field is called next_event: The erb line looks like this:
<%= f.input :next_event, as: :hidden, label: false %>
I want this string to be the datetime in the user's local time zone. If I set a breakpoint in the controller code, I can see that the variable is in the correct time zone (in this case, MDT):
:next_event => Wed, 28 May 2014 15:30:00 MDT -06:00,
However, when it gets rendered in HTML, the date time is converted to UTC:
<input class="hidden" id="foo_next_event" name="foo[next_event]" type="hidden" value="2014-05-28 21:30:00">
What can I do to get simple_form to stop converting to utc?
You can manually specify the value so that the TimeZone conversion doesn't take place:
<%= f.input :next_event, as: :hidden, label: false, input_html: { value: f.object.next_event.to_s } %>
Another option... may be to use the *_before_type_cast method for your attribute. So in the model define:
def next_event_before_type_cast
self[:next_event].to_s
end
With this approach you should get the result you want without having to worry about formatting of the date in forms here (or elsewhere).

Rails 4 : wrong time in edit view with string field and TimeZone per request

I develop an application in Ruby on Rails 4 with TimeZone per request.
I want to use a datetime picker (http://xdsoft.net/jqplugins/datetimepicker/) in my application to replace the default Simple_form datetime input (5 combo-boxes...).
For this kind of datetime picker (I search for others, it's the same), in my view, I have to use a "string" field, like this :
<%= f.input :done_at, as: :string, input_html: { :data => { :behaviour => "datetimepicker" } } %>
When I post the form, Rails take care of the timezone and store in the database the time in UTC.
For example, I put "2014-03-14 19:45:07" (local time is Paris, so UTC +0100) in the field, and I have "2014-03-14 18:45:07" in the database, in UTC. It's correct.
But when I want to edit the information, Rails fill in the field with a wrong time. The offset timezone is lost and I have "2014-03-14 18:45:07" in the field (the UTC time), so 1 hour before the correct time.
How can I have the correct time taking care of the user timezone ? (not in UTC)
I tried the solution found on http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/ to override the display of dates, but it doesn't work.
def done_at
super.in_time_zone(time_zone) if super && time_zone
end
If in my view, I put #action.done_at, the time is correct but not in the field.
Thanks,
Fred
Set the value of the input explicitly. You can move #object.done_at.in_time_zone(time_zone) to a helper if you want
<%= f.input :done_at, as: :string, input_html: { :data => { :behaviour => "datetimepicker" }, :value => #object.done_at.in_time_zone(time_zone).strftime('%d-%m-%Y %H:%M') } %>

Ruby on Rails: formatting date inside a field

My field:
<%= f.text_field :expires_at, :label => false, :class => "input-field" %>
but I want the date to be kinda like this when the page loads: June, 1st, 1752 9:54:00 pm
How would I do that?
Why are you using a text_field for a datetime? Consider using time_select instead.
If you really want to format a date that way though, just use strftime.
So, in your case, add
:value => #object.expires_at.strftime('%B %d, %Y %H:%M:%S %p')
You can format dates and times using the strftime method.
See Ruby's strftime and then use :value => #date_value
If you want this date format to be used throughout your application, you can set the default format in your environment.rb file:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%a %m/%d/%Y %I:%M%p")
If you do this, every time you display a date, it will be formatted according to the date format string you've provided.

Resources