simple_form a single non-model field - ruby-on-rails

So I have a simple_form, and I want to use the standard j-Query datepicker to select the date.
Problem is, rails doesn't like the normal US MM/DD/YYYY date format, so I want to have the field for the display date and store the rails-compliant date format in a hidden field.
How do I tell simple_form to use a field that doesn't exist in the model?

Not sure what you can't do, but i do something like:
$(".datepicker").datepicker({ altFormat: "yy-mm-dd", dateFormat: "yy-mm-dd" });
If configuring the datepicker doesn't work for you, use attr_accessor :new_field_name in your model and add the datepicker to it and do what you were going to do there.

Related

Datepicker chosen date format to Rails backend

I have a Rails 4 app in which we have to present the user with a datepicker but the chosen date displayed should be in MM-DD-YYYY format. When sending the date to Rails, ActiveRecord does not recognize the format as, I know, it is expecting YYYY-MM-DD. Validations fails (date is not present).
The JS is as follows:
$("#some_date").datetimepicker
pickTime: false
format: 'MM-DD-YYYY'
(the display works great with a superb datepicker that appears).
Then my slim template:
= f.input :date, as: :string, label: false, input_html: { class: 'form-control', value: #visit.date.strftime('%m-%d-%Y') }
p.help-block MM-DD-YYYY
If I change the format to YYYY-MM-DD it works perfectly and date is saved into PostgreSQL as is, however in the US, the date is usually displayed as MM-DD-YYYY.
I also don't want to get too involved with helper methods or converting the date from html inputs on each controller so PostgreSQL is happy. I was wondering if there is some way the date format can be changed as it goes to the backend?
Within my config/locales/en.yml, I do have:
date:
formats:
default: '%m/%d/%Y'
Cheers!
Bootstrap datetimepicker doesn't seem to have the option for an alternative format to send to the server. But if all you need is a datepicker and you don't care about time, you can try JQuery datepicker. It allows you to send one dateFormat to display, and an altFormat to send to the server.
In jquery, there is a field called altFormat you can use. The database column doesn’t include time zone information, but the time zone is implicitly UTC. You can use the model method to get the desired format. Instead of saving DB in a specific format I can use model method parse a specific format.
def your_date
self[:column_name].strftime("%m-%D-%Y")
end
and also you want to change time according to user time zone before parsing into the format.
user_record = db_query.your_date
current_user.zone.parse(user_record)
Here are some references might helpful to you.
http://brendankemp.com/essays/dealing-with-time-zones-using-rails-and-postgres/
https://apidock.com/ruby/DateTime/strftime
PS: If you find some other better way plz do post.

Working with Times and Dates in Rails

I have a form that is using timepicker addon to select date and time.
My model have a column:
datetime :start_time
When the forms get submitted params hash has:
params[:start_time] #= '08/31/2014 10:50' as a string
What would be a best way to save it in to the database as a datetime format?
I don't fully understand the way how the time is handled, localizations, time zones etc.
When I tried to save it with out formatting, as a string, I am getting argument out of reach error. When I created new Time instance passing params[:start_date] as an argument it was saved as:
0008-01-01 00:01:15 UTC
TY.
You could also try tweaking the timepicker so the client-side will give rails the desired formatting.
This way you only need to config your timepicker once and you won't need to parse the time in the server side every time
Something like:
$(document).ready(function () {
$('#timePicker').datetimepicker({
dateFormat: "dd-mm-yy",
timeFormat: 'hh:mm ss z'
});
});
Check this jsFiddle for a demo
you could write something like this, for getting correct date from your string:
date_time = DateTime.strptime(params[:start_time], '%m/%d/%Y %H:%M')
date_time.to_s #=> "2014-08-31T10:50:00+00:00"

Convert standard rails / ruby time to American format - dates getting mixed up

I'm trying to convert the YYYY-MM-DD format in my forms and show views to MM/DD/YYYY - which is a pretty standard American date format. Not sure how to go about this - I thought that calling .strftime("%m/%d/%Y") on the date instance in /show would do the trick. Very confused - can't find documentation that explains how to convert dates that actually makes sense. I'm following this railscast: http://railscasts.com/episodes/213-calendars-revised
If it helps, I'm using the jQuery datepicker and have this coffeescript:
jQuery ->
$('input.datepicker').datepicker()
dateFormat: 'mm-dd-yy'
weekStart: 0
autoclose: true
_form
<%= f.label :due %><br />
<%= f.text_field :due, { :class => 'datepicker' } %>
The above enters the date with the MM/DD/YYYY format into the form, but it is saved as YYYY-MM-DD
--- UPDATE/SOLUTION FOUND ---
Turns out I was on the right track and not actually crazy. Part of my issue was that I was having a nil object problem which was remedied by calling .try in my views which will only run the method if not nil.
.try(:strftime, "%m/%d/%Y")
Also - I installed the following gem: gem 'american_date' which automagically parses the dates. Works great in show views, but still getting YYYY-MM-DD in form views (edit view only).
We have to solve three tasks when working with dates in RoR. I found these solutions as the most easiest for me:
1.How to display the date correctly.
Install I18n gem and check date format in your config\locales\en.yml. Then use I18n.l() (or just l) form helper to localize dates in your html.erb files.
date:
formats:
default: "%m/%d/%y"
2.How to modify date in convenient way for user.
Use jQuery datepicker (or bootstrap datepicker etc.) and set date format in jQuery widget options in accordance to en.yml format.
3.How to convert date from localized (view) format to activerecord attribute.
Install Delocalize gem and add :input sub-section into date: section of en.yml file.
input:
formats:
- :default
Now your date will be automatically converted from localized format (mm/dd/yy) into database format (yyyy-mm-dd) when you assign it.
I have had to use the altField and altFormat options to get this to work as described:
$('input.datepicker').datepicker({
dateFormat: "mm/dd/yy",
altField: alt_field,
altFormat: "yy-mm-dd"});
This requires having a hidden field alt_field that actually holds your date in the standard db format.
include the below gem, it will automatically save the american date into db format.
gem 'american_date'

Date Output in Rails using jquery ui

I have a form where you click in start_date and a jquery calendar comes up to pick the date. I want to display the date as (ex.) September 25, 2012, but I also need rails to comprehend the correct start date not just output it correctly.
What do i need to add to this?
<%= feed_item.start_date %>
events.js.coffee
jQuery ->
$('#event_start_date').datepicker
dateFormat: 'yy-mm-dd'
$('#event_end_date').datepicker
dateFormat: 'yy-mm-dd'
Take a look at the altField and the altFormat options for the datepicker.
You can store the Rails-friendly date in a hidden field and pass it to the controller, and display the user-friendly field to the user.
EDIT
Take a look at this answer for an example on how to work with altField and altFormat.

Rails and query datepicker, how to get date in field more tied to pop-up calendar

The text field and the pop-up are linked in that I can use the calendar and the field shows the correct value (or blank) initially.
But they are not linked in that the pop-up is always for the current month, not the month of the db field value.
= f.text_field :content_date, id: 'datepicker', size: 10
I tried adding ,value: #user.content_date but it didn't help.
My jquery is:
// jQueryUI Date Picker:
$(function (){
$(".datepick").datepicker({ dateFormat: "yy-mm-dd"});
});
I added the dateFormat per dimuch which seemed promising but it didn't help yet.
I also tried altFormat but it did't help.
You probably have different date formats in the datepicker and content_date field. Take a look at this fiddle http://jsfiddle.net/wKNXx/

Resources