#217 open
Locv39KnvwDeXGpKpisHDaKw

Proposed fix for CheckboxField

Reported by Locv39KnvwDeXGpKpisHDaKw | April 11th, 2009 @ 09:31 PM

I was having an issue with Webrat's handling of checkboxes. For some reason Rails was seeing a single checkbox as checked when it wasn't. Specifically, Rails was accepting a form submission, but it should've been redirecting back to the form. (The checkbox is for a terms of service agreement.)

After making some minor changes in field.rb it seems like it's working as expected. Any thoughts on the code below?

webrat/core/elements/field.rb:

  class CheckboxField < Field #:nodoc:

attr_reader :checked

def initialize(*args)
  super
  pre_checked = Webrat::XML.attribute(@element, &quot;checked&quot;) == &quot;checked&quot;
  @checked = pre_checked ? &quot;checked&quot; : &quot;&quot;
  if pre_checked
    set(Webrat::XML.attribute(@element, &quot;value&quot;) || &quot;on&quot;)
  else
    set(&quot;&quot;)
  end
end

def self.xpath_search
  &quot;.&#47;&#47;input[@type = &#39;checkbox&#39;]&quot;
end

def to_param
  return nil if @value.nil?
  super
end

def check
  raise_error_if_disabled
  @checked = &quot;checked&quot;
  set(Webrat::XML.attribute(@element, &quot;value&quot;) || &quot;on&quot;)
end

def uncheck
  raise_error_if_disabled
  @checked = &quot;&quot;
  set(&quot;&quot;)
end

def checked?
  @checked == &quot;checked&quot;
end



protected
def default_value
  if Webrat::XML.attribute(@element, &quot;checked&quot;) == &quot;checked&quot;
    Webrat::XML.attribute(@element, &quot;value&quot;) || &quot;on&quot;
  else
    nil
  end
end



end

Comments and changes to this ticket

  • gaffo

    gaffo May 7th, 2009 @ 06:42 PM

    • Tag changed from check, checkbox, field, patch to check, checkbox, field, patch, verify
    • State changed from “new” to “open”

    Could you please resubmit this as a patch or a pull request. We can't tell what version of the file you pulled this from and so can't tell what you changed.

    Thanks

  • gaffo

    gaffo June 15th, 2009 @ 12:20 AM

    • Tag changed from check, checkbox, field, patch, verify to check, checkbox, field, patch, stale, verify

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket

Pages