I have a checkbox
<%= f.check_box :anonymous %>
And my table has a column anonymous which is true or false.
Code generated in html:
<input name="comment[anonymous]" type="hidden" value="0" />
<input id="comment_anonymous" name="comment[anonymous]" type="checkbox" value="1" />
Now, for some reason when I add data it's not saving if my anonymous checkbox is checked or not.. it's not changing data in database.. All other fields gets saved except anonymous.
What can be the problem ?
Use #check_box_tag instead:
<%= check_box_tag(:anonymous) %>
From the official guides:
Array parameters do not play well with the check_box helper. According
to the HTML specification unchecked checkboxes submit no value.
However it is often convenient for a checkbox to always submit a
value. The check_box helper fakes this by creating an auxiliary hidden
input with the same name. If the checkbox is unchecked only the hidden
input is submitted and if it is checked then both are submitted but
the value submitted by the checkbox takes precedence. When working
with array parameters this duplicate submission will confuse Rails
since duplicate input names are how it decides when to start a new
array element. It is preferable to either use check_box_tag or to use
hashes instead of arrays.
Related
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.
I'm making an input form whose features don't fit so nicely into the general form_for template rails provides. I figured I would write my own HTML mimicking the html output of form_for, but embedded with other form_for elements.
For fields I could use the rails framework for I did. For the others I made hidden fields to store what was going to Rails, and regular input fields whose values I manipulated with JavaScript to put into the hidden fields.
This is one such field:
State:<br>
<input type="text" class = "state name_input administrative_area_level_1">
<div class="field">
<input type="hidden" name="address[state]" id="state">
</div>
When I send the value of the hidden field to the console, I get a good response:
state 37
Which means the state field holds the value 37. Perfect.
Except that when I try to make my model, the params come in empty:
"state"=>"",
I do have one field that works that isn't hidden:
Street:<br><input type="text" id="street" name="address[street]">
So I changed the state input type to number, which is what it would be if it weren't hidden, but same result. Why can't rails find my param?
You can show post more detail. I can't understand 100% things you do because you write pretty simple. But i guess that you need test params name is address[state] but it's not only state becase if only state of course it nil.
if html of you rendered same up then i suggest you should put one debug in controller to see params return controller when submit form.
For example: in controller you add line:
logger.debug params
to see right params that form send.
If your form is not tied directly to a object model, it might be difficult to use the form_for helper. In those cases you should use form_tag helper. Read more about form_tag helper here.
Refer this stackoverflow answer for the difference between form_for and form_tag helpers.
Hope this helps.
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. :)
For some reason an html helper is outputting this html which doesnt validate.
the validator tells me
There is no attribute "Length"
<%= Html.CheckBox("Medicamentos", Model.Medicamentos) %>
is outputting
<input type="checkbox" value="true" name="Medicamentos" id="Medicamentos" checked="checked" length="4">
I assume that it's matching the signature that takes a string and an object since I don't know what Model.Medicamentos is. In that case it takes the properties of the object and turns them into attributes on the element. I suspect that you simply want to use the Checked attribute on the Model property specified as the default value of the checkbox, i.e.,
<%= Html.CheckBox( "Medicamentos", Model.Medicamentos.Checked ) %>
In, which case, assuming that Checked is boolean it will match the correct method signature on the helper extension.
Basically I have a set of checkboxes that are dynamically created from view data like so:
<input type="checkbox" name="Calendars" value="<%= c.ID %>" /><%= c.Name %>
The value being the Calendar Id.
I can get what checkbox has been brought back using the FormsCollection its messy but it works!
(There also seems to be a bug with the checkbox helper that renders a hidden field next to the checkbox which means true is actually returned as "true,false"! I can work around this so its not an issue just thought Id mention it)
The problem comes when trying to hook the checkboxes up on an edit page!
I have a schedule class which can have multiple calendars and I want to show which calendars a schedule has by checking them on the edit!
My view is strongly typed but MVC magic can't map this!
Any ideas on whats the best way to do this??
I had tried passing the calendar ids in ViewData and do some inline code to check the appropriate checkbox but this is getting messy!
Thanks!!
UPDATE:
Done this
s.ID == c.ID).Select(s => s).Count() > 0) ? "checked=checked" : "" %>
You need to add "checked" tag manually to every check box:
<input type="checkbox" name="Calendars" value="<%= c.ID %>" checked="checked" /><%= c.Name %>
You dont need <input type="checkbox" - use Html.Checkbox(). It renders a hidden field next to the checkbox - but it is not a bug. From ASP.NET MVC source, InputExtensions.cs, line 201:
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
Use this:
<%= Html.CheckBox("Calendars", c.ID) %>