How do checkboxes work in Rails? - ruby-on-rails

I was wondering how check boxes work in Rails? What would the table be inside of the database( integer, string,etc)? How would you give 3 different values to check boxes and the user only can choose 1(favorite color: red,green or blue)?
I'm new to rails and it would help to have an explanation from the beginning to end as i see a lot of examples but they don't explain everything from the start.
Thank you.

There is a distinction between checkboxes and the database. Checkboxes are HTML. Database is connected to your Models, and it has nothing to do with checkboxes.
When you use a checkbox in your HTML view, your form will send some parameter. By default this parameter will have the value "1" (as String). Rails helpers also create an additional hidden input, which sends the value "0" with the same name as the checkbox input. Parsing the parameters Rails picks the first value, so the given parameter is assigned a value "1" if the checkbox has been checked, and the value "0" if it has not.
Now, the value saved in the database depends on the type of the attribute in your model. If you defined a given field as boolean, then it will be stored as boolean (there is some magic, since the String "0" is not considered 'false' in ruby), if you define the attribute as integer, then it will have the value 1 or 0, and if it's a String, you will have "1" or "0".
About these 3 values for checkbox, I would use a <select> or a radio-button.
Red: <input type="radio" name="colour" value="red" checked="checked"/>
Green: <input type="radio" name="colour" value="green" />
Blue: <input type="radio" name="colour" value="blue" />
See the ActionView::Helpers::FormHelper#radio_button method.

Related

Input Fields Not Displaying Data in Microsoft Edge

I have an ASP.NET MVC 5 application using Razor. When it loads, it displays saved data in input fields. This works fine in IE 11, but when viewing in Microsoft Edge 90.0.818.51, the values are not being displayed.
When I view the source I see the value is set, but am not sure why it's not displaying in Edge. Has anyone been experiencing this?
<input class="small-input form-control text-right" data-val="true" data-val-regex="The field must match the regular expression '^[-+]?(\d+(\.\d*)?|\.\d+)[%]?$'." data-val-regex-pattern="^[-+]?(\d+(\.\d*)?|\.\d+)[%]?$" id="SignificantActivityInputs" name="SignificantActivityInputs" type="number" value="10%" />
There are other fields that are displaying the text. The difference I can see with them are:
They have an associated field that is of type "hidden"
The "main" field is of type "text" where the ones that aren't displaying are of type "number" as shown above.
Has anyone else experienced this?
The type should be text, instead of number
<input class="small-input form-control text-right"
data-val="true"
data-val-regex="The field must match the regular expression '^[-+]?(\d+(\.\d*)?|\.\d+)[%]?$'."
data-val-regex-pattern="^[-+]?(\d+(\.\d*)?|\.\d+)[%]?$"
id="SignificantActivityInputs"
name="SignificantActivityInputs"
type="text"
value="10%" />

Umbraco Forms PreValue sources issue

I'm using a prevalue source in an Umbraco Form to render a list of options from a db table. I've noticed in the html markup that the Value Column is placed in both the list value and the label like so:
<input type="radio" name="3e56b737-c3db-4413-9099-844d002fc3c9" id="3e56b737-c3db-4413-9099-844d002fc3c9_4" value="Secondary">
<label for="3e56b737-c3db-4413-9099-844d002fc3c9_4">Secondary</label>
I'd expect the input value to be the Key Column and the label text to be the Value Column. So it appears that the Key Column is completely ignored when rendering the list. Has anyone else encountered this? Am I doing something wrong or is there some other way to retrieve the Key Column value of the selected list item?
Ok I found out how to fix this. Realised the markup is controlled by the partial view in Views\Partials\Forms\Themes\default\Fieldtypes\FieldType.RadioButtonList.cshtml and DropDownList.cshtml.
So the line that reads:
<input type="radio" name="#Model.Id" id="#string.Concat(Model.Id,"_",i)" value="#pv.Value"
needs to read:
<input type="radio" name="#Model.Id" id="#string.Concat(Model.Id,"_",i)" value="#pv.Id"
HOWEVER: this is great for field values from a db table, but won't work for fields where you've specified a hardcoded list of prevalues. Therefore to handle both scenarios the above line becomes:
var valueId = pv.Id == "0" ? pv.Value : pv.Id;
<input type="radio" name="#Model.Id" id="#string.Concat(Model.Id,"_",i)" value="#valueId"
So if the Id is 0, it uses the string value as before (hardcoded prevalue ids are always zero), otherwise it uses the Id from prevalue source Id field.
The caveat here is that this solution assumes you aren't populating any prevalues with a legitimate Id of 0.

thymeleaf: th:value is ignored when using th:field

I've got a form where I want to edit some user data.
So the already stored data is put as th:value and after sending I validate with spring validation and want to give back the form on wrong input. I want the input field to have the value of the input by the user but it always gives me the stored input instead.
That's how an input field looks like
<input type="text" th:value="${product.name}" th:field="*{name}" th:errorclass="fieldError"/>
If the form is loaded the first time the input fields should have the value of the already stored data.
If it's loaded after submit and with an validation error, the input fields should have the value of the user's input.
Is there a way to to that?
Thanks!
Attribute th:field will replace attributes value, id and name in your input tag.
Instead, use plain th:id, th:value and th:name without using th:field. Then you will get what you wanted.
Then it will look like:
<input type="text" th:value="${product.name}" th:name="name" th:id="name" th:errorclass="fieldError"/>
Similar answer is here: How to set thymeleaf th:field value from other variable
Because Attribute th:field is Combination of both th:name and th:value
So, either use this:
<input type="text" th:value="${product.name}" th:name="name" th:errorclass="fieldError"/>
Or this:
<input type="text" th:field="*{name}" "th:errorclass="fieldError"/>
Using th:field or value, id and name is ok. if you user th:field, you can write this:
<input type="text" th:field="${product.name}" th:value="${product.name}" "th:errorclass="fieldError"/>
You have error in your controller (you set wrong value to *{name}), if the input value is wrong after validation error.

Struts2 form parameters with same name and retrieving the values

I've an input fields dynamically added multiple times in the form using jQuery. Onsubmit of the form, at the action side I'm getting "comma+space" separated request parameters & values as shown below,
<input type="text" name="chars" />
Action side --> chars=char1, char2, char3,......
to get the values I can split the string by comma(,), but this case fails if the user enters comma in the text fields. I cannot get the entered comma by any means.
Can anyone suggest on how to separate the values and get the comma as character as I've to store this in DB?

Why doesn't TextBoxFor include validation elements if called twice for the same model property?

Simple question... Here is an example of some razor code:
#Html.TextBoxFor(c => c.RevisedEstimate)
#Html.TextBoxFor(c => c.RevisedEstimate)
Here is how this renders:
<input data-val="true" data-val-number="The field Revised Estimate must be a number." id="RevisedEstimate" name="RevisedEstimate" type="text" value="0" />
<input id="RevisedEstimate" name="RevisedEstimate" type="text" value="0" />
The obvious question you ask is, "Why are you doing that?". The razor view is actually building client side detail-row templates that are used in KendoUI grids. There are two similar grids and we use the same viewmodel server side. We actually do provide the id element for the template so each field in each row ends up with a unique id.
Why does the second input element not have the data-val and data-val-number elements?
Off the top of my head knowing what the JS does in the background, it seems to do this to prevent conflicts. The JS looks for the elements with the data- attributes to do it's validation, along with other functions, so it could possibly pick the wrong one if there are multiple instances of it.
since we were generating HTML for use in a client side template what we did was just create a variable to hold the HTML generated by the helper, and then render out that code in the Views..
Something like:
#{
var revisedEstimateInput = Html.TextBoxFor(c => c.RevisedEstimate)
}
Then later in the view:
#(revisedEstimateInput)
...in as many places as needed. This way the validation and other metadata attributes were in place in our client templates and all the kenodUI validation worked correctly.

Resources