New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkboxes don't work when used with ROR checkbox helper #339

Closed
mlins opened this Issue Oct 30, 2010 · 11 comments

Comments

Projects
None yet
9 participants
@mlins
Contributor

mlins commented Oct 30, 2010

If you use the Rails checkbox helper, it creates an additional hidden input field with the same name/id as the checkbox. See why here:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box

Basically, all that is needed is a more specific selector when refreshing checkboxradio.

@mlins

This comment has been minimized.

Show comment
Hide comment
@mlins

mlins Oct 30, 2010

Contributor

Creating pull request.

Contributor

mlins commented Oct 30, 2010

Creating pull request.

@amiuhle

This comment has been minimized.

Show comment
Hide comment
@amiuhle

amiuhle Jun 8, 2011

I've found a workaround: If you move the f.label behind the f.check_box, this works.

<fieldset data-role="fieldcontain" class="field">
  <%= f.check_box :working %>
  <%= f.label :working %>
</fieldset>

If it's not arranged this way, jQM will move stuff around, so that the rails generated hidden field will be behind the checkbox in the DOM.

amiuhle commented Jun 8, 2011

I've found a workaround: If you move the f.label behind the f.check_box, this works.

<fieldset data-role="fieldcontain" class="field">
  <%= f.check_box :working %>
  <%= f.label :working %>
</fieldset>

If it's not arranged this way, jQM will move stuff around, so that the rails generated hidden field will be behind the checkbox in the DOM.

@joelwreed

This comment has been minimized.

Show comment
Hide comment
@joelwreed

joelwreed Apr 14, 2012

I can confirm this is still a problem with jqm 1.1 and that the workaround above 'fixes' the issue.

I can confirm this is still a problem with jqm 1.1 and that the workaround above 'fixes' the issue.

@sealless

This comment has been minimized.

Show comment
Hide comment
@sealless

sealless Aug 10, 2012

I have confirmed that this behaviour persists in 1.2.0-alpha. I'm not sure I understand why this issue was closed.

I have confirmed that this behaviour persists in 1.2.0-alpha. I'm not sure I understand why this issue was closed.

@johnbender johnbender reopened this Aug 13, 2012

@johnbender

This comment has been minimized.

Show comment
Hide comment
@johnbender

johnbender Aug 13, 2012

Contributor

You can also work around this on the rails side by avoiding mass assignment. This one is going to be low priority.

Contributor

johnbender commented Aug 13, 2012

You can also work around this on the rails side by avoiding mass assignment. This one is going to be low priority.

@ryanilg

This comment has been minimized.

Show comment
Hide comment
@ryanilg

ryanilg Oct 10, 2012

I know this is low priority, but I just wanted to note that a colleague and I also ran into this issue as well on 1.2.0.

ryanilg commented Oct 10, 2012

I know this is low priority, but I just wanted to note that a colleague and I also ran into this issue as well on 1.2.0.

@stereoscott

This comment has been minimized.

Show comment
Hide comment
@stereoscott

stereoscott Nov 29, 2012

+1, my checked values where getting submitted to the server as empty every time so I changed the use of the form helpers to the manual check_box_tag.. not quite as convenient but it works

+1, my checked values where getting submitted to the server as empty every time so I changed the use of the form helpers to the manual check_box_tag.. not quite as convenient but it works

@nathany

This comment has been minimized.

Show comment
Hide comment
@nathany

nathany Apr 17, 2013

I can confirm that this is still an issue in JQM 1.3.1. @amiuhle suggestion of having the label after the checkbox works, but that really shouldn't be necessary.

nathany commented Apr 17, 2013

I can confirm that this is still an issue in JQM 1.3.1. @amiuhle suggestion of having the label after the checkbox works, but that really shouldn't be necessary.

@nathany

This comment has been minimized.

Show comment
Hide comment
@nathany

nathany Apr 17, 2013

@stereoscott Using check_box_tag means Rails won't add the hidden field, which means unchecking a checkbox won't post a '0' value to the server, so checkboxes won't ever uncheck. On the other hand, using check_box after a label won't post a '1' value to the server, so checkboxes can't be checked. Label after and it works, but that's a confusing requirement.

nathany commented Apr 17, 2013

@stereoscott Using check_box_tag means Rails won't add the hidden field, which means unchecking a checkbox won't post a '0' value to the server, so checkboxes won't ever uncheck. On the other hand, using check_box after a label won't post a '1' value to the server, so checkboxes can't be checked. Label after and it works, but that's a confusing requirement.

@jaspermdegroot

This comment has been minimized.

Show comment
Hide comment
@jaspermdegroot

jaspermdegroot May 1, 2013

Member

Can somebody clarify for me what exactly is the bug that needs to be fixed here? What is it that jQuery Mobile does with that hidden field that causes the problem?

Member

jaspermdegroot commented May 1, 2013

Can somebody clarify for me what exactly is the bug that needs to be fixed here? What is it that jQuery Mobile does with that hidden field that causes the problem?

@nathany

This comment has been minimized.

Show comment
Hide comment
@nathany

nathany May 1, 2013

@uGoMobi Here's the relevant Rails documentation for checkboxes.

It outputs a hidden field followed by a checkbox of the same name. The order matters. If the checkbox isn't checked, the value of the hidden field becomes the default. If the checkbox is checked, then that value is used. I haven't dug into what exactly what JQM is doing, but when it's augmenting a checkbox, these two fields can end up in the wrong order. If the hidden field comes after the checkbox, the checked value will never be used.

Again, I'm not entirely sure why placing a label after the checkbox has an impact on JQM's behaviour. The problem is that the location of the label in the code does not impact how the checkbox is rendered, so it looks like it will work, even when it doesn't. It's also the case that every other control typically has the label before the form field, so it seems natural to put the label before a checkbox (given the same rendering).

nathany commented May 1, 2013

@uGoMobi Here's the relevant Rails documentation for checkboxes.

It outputs a hidden field followed by a checkbox of the same name. The order matters. If the checkbox isn't checked, the value of the hidden field becomes the default. If the checkbox is checked, then that value is used. I haven't dug into what exactly what JQM is doing, but when it's augmenting a checkbox, these two fields can end up in the wrong order. If the hidden field comes after the checkbox, the checked value will never be used.

Again, I'm not entirely sure why placing a label after the checkbox has an impact on JQM's behaviour. The problem is that the location of the label in the code does not impact how the checkbox is rendered, so it looks like it will work, even when it doesn't. It's also the case that every other control typically has the label before the form field, so it seems natural to put the label before a checkbox (given the same rendering).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment