optional date field with simple_form - simple-form

I am using simple_form to generate date input like so:
= f.input :date_of_death
However, it always pre-populates with the current date.
How can I have it come up "blank" and allow the user to easily NOT enter a date?
Thanks!

There are a couple of ways to do this while still keeping the form split into dropdowns for Y/M/D.
= f.input :date_of_death, include_blank: true
Or if you want the first option to have text use prompt.
= f.input :date_of_death, prompt: "Choose Wisely"

Simplest way that I have found
f.input :date_of_death, as: :string
It will turn the input into a blank input field. You will have to parse the string as a date in your controller.

Use include_blank
= f.input :fecha_at, include_blank: true, start_year: Date.today.year - 90, end_year: Date.today.year

Related

Simple form - Keep radio buttons checked when edit

I have few radio buttons; the issue is that when I edit the instance, the radio buttons are not checked anymore (however they have been saved in db when I created it). I would like that the radio buttons the user previoulsy checked, to be still checked when edit.
_form.html.slim
.cov-pick-row.w-row
- #inspiration_images.each do |img|
- next if img.image.path.nil?
.w-col.w-col-2.w-col-small-4.w-col-tiny-6
label
INPUT[type="checkbox" name="cover[inspiration_image_ids][]" id='cover_inspiration_image_ids' value="#{img.id}" class="hidden"]
.image-cov-pick
= cl_image_tag(img.image.path, height: 190, class: 'img-to-pick', data: { ix: "cover-pick" }).html_safe
Set checked: true for which it should be check. Ex. assume you have db column check_status where you store checked value then use checked: img.check_status.
From your sample code as much as I understand that you are saving image id on inspiration_image_ids so use can use inspiration_image_ids.include?(img.id)
Use checked: [#your ids]
Example :
create_table "mymodel", force: :cascade do |t|
t.string "interest_ids", default: [], array: true
end
# _form
<%= simple_form_for(#mymodel) do |f| %>
<%= f.input :interests_ids,
as: :check_boxes, # Display check_boxes
collection: Interest.all, # Search into ids collection
value_method: :id, # Save only the ids
checked: #mymodel.interests_ids, # keep and check the old values
input_html: { multiple: true }
%>
...

Phoenix.HTML.Form date_select/3 for optional date

The problem
I have a form where I have a field for an application date. This value is optional, as it can be provided at a later time (i.e. it is not necessarily present when the form is saved).
Using date_select/3, I'm unable to find a simple way to have this value be optional. Even if default: nil is provided as an option, the current date is filled in the fields. This is not the desired behavior, as when the user saves the form the value would be updated also.
What I'd like
A field to enter a date, with the possibility of leaving it blank (which would be the default behavior).
What I've got so far
You can get a date field for an optional value using the builder option and using empty prompts:
<%= date_select f, :applied_on, builder: fn b -> %>
Date: <%= b.(:day, [prompt: ""]) %>.<%= b.(:month, [prompt: ""]) %>.<%= b.(:year, [prompt: ""]) %>
<% end %>
Isn't there a better, more elegant way to handle optional dates?
If the field is required or not should handled by your validating, default in model part by ecto.
From Phoenix.HTML.Form.datetime_select/3:
:year, :month, :day, :hour, :minute, :second - options passed to the underlying select. See select/4 for more information. The available values can be given in :options.
From Phoenix.HTML.Form.select/4:
:prompt - an option to include at the top of the options with the given prompt text
You need only to set the prompt (or other select-specific options) to the field option (:year, :month, :day, :hour, :minute and :second) of date_select. This should work:
<%= date_select f, :applied_on, year: [prompt: ""], month: [prompt: ""], day: [prompt: ""] %>
This will work for datetime_select/3, date_select/3 and time_select/3.
Hope this helps.

Date Format in form page using Rails 4 with Haml

I am using Rails 4.1.6 and Ruby 2.1.5 and for view I am using html.haml file.
In Form Page I want input date format should be in mm/dd/yyyy format.
For this in in config/locales/en.yml file i have:
date:
formats:
long: ! '%-m/%-d/%Y'
In _form.html.haml I have:
f.input :date_of_birth,:input_html =>{:format => :long}
The problem I have is it's not working. Any suggestions?
Try -
= f.input :date_of_birth, :value => t(#model.date_of_birth,:format => :long)

populate dropdown with two fields in rails 4 ajax

I am having trouble populating a field with two columns concatenated. For example: I want to populate Worker field with both first name and last name in AJAX rails 4.
Currently I'm populating with just first name using
<%= collection_select(:work_order ,:worker_id ,Worker.all, :id ,:first_name) %>
You can do something like:
<%= f.input :user_id, :as => :select, :collection => [[current_user.firstname+' ' +current_user.lastname, current_user.id]], :label=>"Names"%>

Iterate Form fields

I have build a module to add translations for each standard topic. Theses topic got many standard options and you can translate it directly in page.
I got an issue with my form about the edit view.
When i display a translation it's repeat all value of the f.input :value each time he have one and i want it to display with the each of standard value.
The question is how i can iterate my input field :value in the form to display only once per standard value and not repeat all value translated by standard value.
when i want create a new one all workings fine. It's just about the iterate field who is repeated how many times he got a field in the table.
the gist for my code :
https://gist.github.com/266562670cd8dab28548
Change:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first, option do |translate_form| %>
to:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first || option.preference_topic_option_translations.build, option do |translate_form| %>

Resources