Empty value in activeadmin for checkboxes list - ruby-on-rails

I use checkboxes list for enum to select multiple values in activeadmin:
c.input :weekdays, as: :check_boxes, collection: BusinessHour.weekdays.keys
For some reason there is hidden value added:
<input type="hidden" name="listing[listing_prices_attributes][0][weekdays][]" id="listing[listing_prices_attributes][0]_weekdays_none" value="" autocomplete="off">
So when form is submitted it actually adds also empty value "" in array and makes problem.
Tried also add include_hidden: false, include_blank: false but it also doesn't change empty string value.
Any reason why it works this way?

Related

rails check_box_tag default unchecked value

Is there any way to set check_box_tag unchecked value to any other value than nil? Something like:
check_box_tag :active, ['true', 'false'], ...
In the HTML specifications unchecked checkboxes are not included when sending the form. So there is no "unchecked value" for a checkbox.
There is a workaround though - you include a hidden input with the same name attribute as the checkbox:
<input type="hidden" name="active" value="deader then dead">
<input type="checkbox" name="active">
Since key/value pairs have to be sent in the same order they appear in the form the checkbox will override the hidden input if it is checked.
The higher level Rails checkbox helper has this workaround built in.

Simple Form: checkbox's value is always "1"

I have a form and need to add a GDPR checkbox.
If the checkbox is clicked (= checked), the checkbox's value should be true (or 1). If not, then the value should be false (or likely nil).
This is how I render the checkbox:
= f.input :gdpr, as: :boolean, required: true, label: false, checked_value: true, unchecked_value: false, input_html: { }
However, when I look at the generated HTML, I see that (probably) by default the checkbox's value is 1 - how is that possible?
EDIT: This is the rendered HTML:
<input name="obj[gdpr]" type="hidden" value="open">
<input required="required" aria-required="true" type="checkbox" value="true" name="obj[gdpr]" id="obj_gdpr">
How do replaced that 1 with nil?
Thank you in advance.
value attribute is not the default value of the checkbox. This attribute is submitted as input's value to the server if checkbox is checked.
E.g.
<label>Completed?</label>
<input type="checkbox" value="completed" name="status"
This will be submitted as status=completed.
To check if checkbox is checked use checked attribute of DOM node in JS.
Also see MDN explanation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value

How does the argument "value" in check_box_tag work?

The docs for check_box_tag are as follows:
check_box_tag(name, value = "1", checked = false, options = {}) public
Creates a check box form input tag.
What is the value argument?
I have provided many different values to the value parameter in my own checkbox form, yet the checkbox seems to work the same (it works, I just want to know what value is doing?).
It seems some others are also confounded by it.
Example
These do the same thing in my application (they both work perfectly, which I wouldn't have expected):
<%= check_box_tag :approved, "hi there", user.approved? %>
<%= check_box_tag :approved, "1", user.approved? %>
The value argument refers to the boolean value in the db.
If the checkbox is unchecked then the value will be '0' and the boolean will be set to false
If you check the checkbox then the value will be '1' and the boolean will be set to true
This is the way checkboxes work in Rails and it doesn't mind if you want to pass a custom value like value='ilovemydog' or something else, it will always refer to '0' or '1'.
I don't have much experience with ERB but the method gets converted to simple HTML as
<input id="accept" name="accept" type="checkbox" value="1" />
The value attribute is used when using the input tag with forms, now when the form is submitted and the checkbox is checked, the value is passed as "accept=1"

How to remove area-require attribute in rails form?

This is a checkbox which has required attribute true
This form belongs to form component made using simple form
= form.input :terms, label: "hello", as: :boolean, required: true
But when this is converted into HTML (rendered by RAILS), we get this result
<input class="boolean required" required="required" aria-required="true" type="checkbox" value="1" name="sample_name" id="sample_id">
The above code contains an extra attribute called aria-required which comes with require attribute from HAML file.
It is said that simple form automatically adds the this aria-required attribute.
How to remove the aria-required attribute from the form without removing required attribute?

Rails Simple Form single checkbox twice in querystring

I have a search form which I return to after searching, to be able to refine the search. I just added a checkbox to that form.
In the view:
<%= f.input :available_tomorrow, as: :boolean, label: false,
inline_label: t('public.search.form.available_tomorrow.label'),
input_html: { name: :available_tomorrow, value: params[:available_tomorrow],
id: :available_tomorrow } %>
In the model:
attr_reader :available_tomorrow
HTLM produced:
<div class="form-group boolean optional search_available_tomorrow">
<input name="available_tomorrow" type="hidden" value="0">
<label class="checkbox">
<input class="boolean optional" id="available_tomorrow" name="available_tomorrow" type="checkbox" value="1">
Available tomorrow
</label>
</div>
When I check the box, all search parameters appear fine in the url querystring but that one:
&available_tomorrow=0&available_tomorrow=1
Looks like the value property of both field is sent, and neither changes. If I uncheck the box, I only get &available_tomorrow=0 in the querystring. The second part is only added if the checkbox is checked.
Everything works as intended (the search does return the right results depending on the checkbox state, the checkbox is in the right state when the search form is updated). But that querystring is ugly with both available_tomorrow parameters, looks like the first one should never appear. Ideas?
This is normal behavior for a form with a checkbox when you POST the form: If the checkbox is not checked, only the hidden field with value 0 is sent with the formdata. If the checkbox is checked, the input with the value 1 is also sent.
The hidden field for the checkbox is always sent, this is because otherwise no value for the checkbox would be sent.
Long story short: don't mind the querystring in your search url, nobody will ever look at them. :)

Resources