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.
Related
I am using a text_field to enter 24 hour time on a form. The regular expression works for everything but and empty string. Why?
<div class="field">
<%= f.label :start_tod %><br>
<%= f.text_field :start_tod,
value: (tod_to_str(#availability.start_tod) || '00:00'),
pattern: '([01][0-9]|2[0-3])[:|\.][0-5][0-9]|24[:|\.]00',
title: '00:00 to 24:00' %>
</div>
To check the 24 hour text format, as HH:mm you can use the following regex:
([01]?[0-9]|2[0-3]):[0-5][0-9]
in the first part you have one group, that will handle hours separetely, with rules for hours started with 0 and 1. The last part if for the numbers.
You should also add some validation on your model, based on the same regexp, and decide if you want to save an empty string or a nil value when the user don't fill anything.
I have a form which allows user to select date, in the view i have it so the user only selects the year, but when the date is saved, it saves todays day and month along with it? all I want it to display is the year, which the user selects, this is my code:
<%= f.date_select :finishdate, :order => [:year] %><br>
If you wish to show only the year value in the form and store it in the database without the month and day, then you can have an integer field and only show the year value as follows:
<%= f.select :finishdate,Date.today.year-10 .. Date.today.year+10 %>
you can try
<%= f.select_year :finishdate, :order => [:year] %><br>
What is the end result that you'd like to work with? If it is only the year, you'll probably not want to use the date_select as that, I believe, will return a DateTime instance and not just the year.
If you do need just the year, you can look into other helpers like select_year.
I have a form field in my Rails view:
<%= f.date_select :Date_of_Birth %>
This shows a drop down menu with only the last 10 years and I need to go back to 1800. Better if I could just type the year in rather than have a 200 item drop down list. I also need the option not to fill in the date, that is, blank.
I have found a couple of hints here in stackoverflow, but they don't use quite this format which was generated for me. Suspect because I'm using Rails 4.
Here's how to extend the range of selection:
<%= f.date_select :Date_of_Birth, start_year: 1800, end_year: Time.now.year %>
Having a text input field for the year is not so easy but it could be done, by adding discard_year: true to the date_select.
Then you add a text field tag AFTER the date_select inputs with a special name like name_of_your_object_Date_of_Birth(1i) where "name_of_your_object" is the name you used in the form_for tag. Inspect the HTML fields which date_selects generates in your browser.
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.
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).