Format date in ActiveAdmin form - ruby-on-rails

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.

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.

ActiveAdmin: display parameter of object selected in form

I am not very familiar with ActiveAdmin or rails in general, so sorry if I use some incorrect phrasing.
I have a model for an Athlete, which has an attribute "notes". This is described in the permit_params of the athlete.rb.
On an additional page, I have the following:
f.input :athlete, :collection => Athlete.all.sort_by(&:last_name), :required => true
I would like to find a way to, if the :notes is not empty, display it.
If I could display it as a "row" on the form, that would be great.
Thanks!

Ruby on rails simple form text field value is overridden by default value

I am using a simple form like which has a text field with a default value. Once the user sets their desired value, the default value should be overridden by the user's desired value. But each time the user opens the form to edit it, they see the default value again and again:
<%= f.input :notes, input_html: {:value => #order_f.decorate.template_message, rows: 12} %>
Instead of setting a value, try setting a placeholder like so:
<%= f.input :notes_to_deliverer, placeholder: #order_f.decorate.deliverer_template_message, input_html: {rows: 12} %>
If you don't need the placeholder then you can do it like this:
<%= f.input :notes_to_deliverer, input_html: {:value => object.notes_to_deliverer.present? ? object.notes_to_deliverer : #order_f.decorate.deliverer_template_message, rows: 12} %>
Here the object is for which you have created the form. Also please make sure that this value is saved in database on form submit. And if suppose this your default value to be stored what I mean is if user doesn't enters any value then you need this to be stored then it would be better to use default value at database end. You can set it in migration file.
Hope this helps.

How can I generate input[type=date] with simple_form?

When I do f.input :start_date, as: :date I get 3 select elements (day, month and year).
I can do f.input :start_date, as: :string to get input[type=text] element, but how can I generate input[type=date] element with simple_form?
You should specify html5 option:
f.input :start_date, as: :date, html5: true
Here is details.
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 :expires_at, as: :date, html5: true %>
Hopefully this piece of information found in simple_form documentation will solve your problem.
You can also set that as the default defining a class that makes that as default:
# Use html5 date inputs by default, use html5: false to force
# the use of multiple selects
class DateTimeInput < SimpleForm::Inputs::DateTimeInput
private
def use_html5_inputs?
input_options.fetch(:html5, true)
end
end

Rails 3 Formtastic. How can I make formtastic display only the month and year fields WITHOUT the day?

I need formtastic to display only month and year fields, WITHOUT the day fields.
The datepicker is nice but it shows the whole calendar. I don't want the datepicker.
f.input :accounting_month, :label => "Accounting month", :as => :datepicker
All I need is the month and year.
There is a way to do this:
<%= f.input :accounting_month, :label => "Accounting month", :order => [:month, :year]
This will automatically hide the "day" input and give it a default value of 1.
try this
f.input :accounting_month, :as => :date_select, :discard_day => true
It doesn't work that way because without day this implies 28-31 (depending on month) days and it could be any one of them. If you use want to store a month-year selection without day you'll see it is a range of dates, not "a" date.
Advice with dates is to always store the whole date as a date field. If you only know month and year you'll need to have two (non-date) fields, one for each. But as you can see it's going to lose you a lot and you'll need to custom craft each field, validate it, etc. major pain so needs pretty good reason to do it..
The only other thing I can suggest is:
create a hidden div, or apply css if you can't 'get inside' the standard date field on the form, for the 'day'. Then set the value to always be 01. Then you might have a 'date' set of fields that will save 01-month-year to the database. This is definitely a 'hack'!
With the new syntax in Ruby 1.9
f.input :date, as: :date_select, discard_day: true

Resources