stop simple_form from converting datetime to utc - ruby-on-rails

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).

Related

bootstrap get crazy date format when editing a date in 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 %>

rails time input shows different format on edit

I have a timepicker that inputs the time like this: 12:30.
After submitting the show of the model also shows the time like this 12:30.
But when i want to edit the file the time shows up like this: 2000-01-01 12:30:00 UTC
How do is set up the correct format fot this problem?
You have to use Ruby in-built time formatting like this:
t = Time.now #=> 2007-11-19 08:37:48 -0600
t.strftime("%I:%M") #=> "08:37"
More info and explanation can be found here
<%= f.input :start_time, as: :string, input_html: { value: resource.start_time && resource.start_time.strftime("%H:%M") } %>
Fixed it by using this(NOTE: I am using inherited resources and simple form)

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') } %>

Rails how to remove UTC offset text ('UTC') from a dateTime leaving only the DateTime?

I have a tool set to store everything in the db as UTC. On a form I have a text field with an attached jQuery Date + Time picker.
In that textfield the dateTime stamp (w/ UTC offset) will just appear like:
<%= f.text_field :report_time, :size => 22 %>
2012-06-05 15:55:50
However I need this form choice to be in EST. So I need to convert it going to the form and then when the form is submitted. This problem is specific only to when an 'EDIT' action is taken (time already exists in field). If I do any addition or subtraction "UTC" gets stuck on the end in the form text field:
#report.report_time = #report.report_time - 5.hours
=>2012-06-05 10:55:50 UTC
At this point a jQuery DateJS dateparse function I'm using will stop working because it doesn't understand the "UTC" text in the text field holding the dateTime.
How can I take a db UTC time and add/subtract hours without "UTC" getting dropped in the form, or how can you remove the UTC offest information leaving only the raw dateTime information?
How do I:
#report.report_time = #report.created_at - 5.hours
=>2012-06-05 10:55:50 UTC
# do something here to strip " UTC" off the dateTime ??
=>2012-06-05 10:55:50
Thank You!
Rails 2.3.5 / Ruby 1.8.7
Use the strftime function to set the value of the field directly, if you don't want the default representation.
<%= f.text_field :report_time, :size => 22, :value =>"...." %>
http://ruby-doc.org/stdlib-1.8.7/libdoc/date/rdoc/DateTime.html#method-i-strftime
You may probably want to check out this link for information on how to use the date/time more naturally with the form builder.
http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers

Rails -- Can't set value for date_select in form

I'm pulling data from an API where the date data comes in as "2008-02-11 00:00:00 "
I would like that data to go into my form within the date_select as a value so I can view it correctly before I add it into my database.
The view looks like
<%= f.label :start_date %><br />
<%= f.date_select :start_date, :value => " #{#stdate[idx]} " %>
The object is actually an array of dates since I'm doing this action several times do thats why the [idx] is there; serving as an index.
<%= #stdate[idx] %> ends up outputting "2008-02-11 00:00:00 " but the fields for the date_select helper only outputs the current date "2010" "June" "5" in those dropdown date selects fields...
Do I need to set the values of the Year, Month, and Date Individually? I have Chronic and tried to parse the object before using it as a value for the date_select and that didnt work either.
Any ideas?
You wouldn't use the :value option but the :default option and pass a DateTime object to it.
There is no :value option for date_select. In your example, the value of the dropdowns will be obtained from the start_date attribute of whatever object you passed in when you started the form builder f.
On this object, you can simply set the start_date attribute before rendering, even if you're not actually saving it there.
There's also a select_date helper, which is the variant that is not linked to an object, and just allows you to pass a value. But that requires more manual labor, because it doesn't work out of the box with update_attributes.

Resources