I am designing a search page in Rails using Ransack and I need some help with check boxes.
I have several check boxes. Here is one of them.
<%= f.check_box :if_bt_eq %>BT
I know that when a check box is ticked it returns 1 and 0 when not ticked.
Is there any way in which I can change the non-ticked return value to nil ?
If not, can you suggest some sort of alternative for this?
Thanks!
Update:
Here's the hashes that are passed when the search form submit button is clicked:
with the check box ticked:
...q%5Bif_bt_eq%5D=1&...
with the check box not ticked:
...q%5Bif_bt_eq%5D=0&...
I want the check box not ticked to be
...q%5Bif_bt_eq%5D=&...
Related
How do I validate a checkbox in FormBuilder? I'm looking for something like checkbox.isAny. I'm working on very complex branching on a survey form. If any are boxes are checked, I will show another question in a survey, and so on.
Right now, my formula is looking for checkbox1 or checkbox2 or checkbox3. That works when any 1 checkbox is checked. But when checkbox1 AND checkbox2 is checked the next question does not appear. There must be a way to check for any boxes.
I'm new, so if you can point me to a wiki or online learning source for xforms validation and xpath, I'd greatly appreciate it.
Thank you
If you want to show a question (say named question) if any of the checkboxes for a field (say named checkboxes) is checked, maybe because if any of the checkboxes is set users need to provide more information, then set the Visibility for question to:
$checkboxes != ''
I have a multiple select box in a form
<%= f.select :receipts, [], {}, multiple: true -%>
I want to ALWAYS post all options that are in the select box, but HTML only posts the ones the user has clicked on.
I could handle the submit event in JavaScript and set all the options to be selected, but I don't really want the user to see the items get selected. The form may fail to post and I'd prefer not to change anything visual on the screen.
The other thought I had was to use a hidden element and set its value to be a comma separated list of items from the multi-select box just before the submit happens. Then I don't have to mess with the selection state of the multi-select box.
Is there some Rails magic I can use instead of doing this?
In #eyecolor contain hash
{"Brown"=>"Brown", "Black"=>"Black", "Blue"=>"Blue", "Green"=>"Green"}.
Now i want to show multiple check box in rails, and i have implement this code for this
<%= check_boxes_tag "eyecolor[]", options_for_select(#eyecolor, params['eyecolor']),{:multiple => true,:size=>5} %>
and i want to check Black
but it didn't show me multiple checkbox, It display me only one check box.
I have a checkbox iagree and when I am submitting the form I am getting
obj[iagree] : 0
obj[iagree] : 1
in the request. I don't know why is this?
This is expected when using form helpers like check_box_tag.
This helper create a hidden field just before the check box with the same name and the value 0.
The reason for this hidden field is to solve the issue, that the browser sends no parameter at all when a check box is unchecked.
So it is not possible to destingish, whether there is no check box in the form or the check box is uncheced.
With this workaround an unchecked check box is returned with value 0 and a checked checkbox with value 1.
The second parameter just overwrites the hidden field, if checked.
In your params[] you will always see obj[iagree] with either 0 or 1.
I want to create an extra radio button with some choices inside an existing rails form (I use simple_form gem). The problem is that I do not want this to point in any field in my model. I want it to be an extra field that I would like to pass.
All examples that I found introduces a hidden tag. But I do not want it to be hidden I want a radio button set.
Is that possible?
Try using radio_button_tag inside the modal form, I have used it in the past. An example would be:
radio_button_tag 'send_me_updates', 'radio', false
When you submit the form, I think you will be able to access the radio button value with params[:send_me_updates]. Should work, please try it and let me know.