Date in Rails form - ruby-on-rails

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.

Related

Activeadmin year select has only 10 years before

I have a Date field, date_of_birth
on active admin resource I use
input :date_of_birth, required: true this gives me a dropdown for the year that has only 10 values, from 2016 to 2026.
How can I change the values for year dropdown?
First of all, you might build collection for the date_of_birth. For example (dirty method, I don't love it):
form do |f|
f.inputs do
f.input :date_of_birth, as: :select, collection: YEARS_COLLECTION
end
end
Where YEARS_COLLECTION might be replaced easily with mock (1990..Date.now.year).to_a, or be calculated in your active_admin controller, or might be fetched from the collection (if you have one): BirthYear.all.
Unfortunately, I can't advise anything else because I don't know what years you want to use.

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.

How do you fix a sinple_form input tag to manually typing an integer instead of having a up/down clicking icon?

I have an input that has the data type of an integer. When I created a form for that attribute, the simple_form gem gives me a up/down click icon thingy on the right hand side of the input box.
I would like to make this a regular input box, which I can manually type in integers.
Below is what I currently have.
<%= f.input :price %>
Simple form detects the field type based on the data type of the field in display. Since what you have may be numaric in nature you will get the numeric input.
You can override this using the as option and requesting it to be a text. Here is how to do it.
<%= f.input :price, as: :text %>
But if you are concerned about the decimal places and arrow increment/decrement, you can configure it to lets say go cent by cent with some thing like
<%= f.input :price, input_html: { increment: '0.01' } %>

Rails collection_select default select

In Rails 4 in view I have
<%= form_for #person do |f| %>
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: "Select your country" %>
...
<% end %>
I'd like "Select your country" to be selected as default whenever the page is loaded. One way is to use javascript (select it after the dom is loaded). Is there an easier way like adding an option to collection_select?
Thanks.
As per the docs, it's the prompt option in the options argument:
collection_select(:post, :author_id, Author.find(:all),
:id, :name_with_initial,
{:prompt => 'Please select the author of this post'})
With collection_select on a form builder we omit the first argument, so in this case:
f.collection_select :country_id, Country.order(:name), :id, :name, {prompt: 'Select your country'}
I've 100% confirmed this as working on my own app running Rails 4.1.6, where prompt and include_blank do the same thing.
The way this works is Rails injects a null-valued <option> as the first item in the generated <select> (this is because the HTML spec has nothing analogous to placeholder on text inputs for select inputs).
Reasons this may fail:
Rails does not mark the prompt option with the selected attribute, and I suspect some browsers may choose to render their own blank entry instead of the first in the list
If, for existing records, Rails determines that the current record's country_id matches an element in the list it will mark that one as selected. This is expected behaviour but can be a pain if you're doing anything non-standard.
If you're being bitten by these problems your options are to build the form manually (the method options_from_collection_for_select may be of use here) or do it in javascript. There is also an undocumented default attribute you can add to an <option> tag but it's not in the spec and browser support may be patchy, and you'd still have to build the form manually.

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