Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Capybara从复选框组中选择元素_Ruby On Rails_Ruby On Rails 3_Rspec_Capybara - Fatal编程技术网

Ruby on rails 使用Capybara从复选框组中选择元素

Ruby on rails 使用Capybara从复选框组中选择元素,ruby-on-rails,ruby-on-rails-3,rspec,capybara,Ruby On Rails,Ruby On Rails 3,Rspec,Capybara,我有很多这样的行: <input id="car_ids_" name="car_ids[]" value="142" type="checkbox"> Car 142 <input id="car_ids_" name="car_ids[]" value="143" type="checkbox"> Car 143 <input id="car_ids_" name="car_ids[]" value="144" type="checkbox"> Car 1

我有很多这样的行:

<input id="car_ids_" name="car_ids[]" value="142" type="checkbox"> Car 142
<input id="car_ids_" name="car_ids[]" value="143" type="checkbox"> Car 143
<input id="car_ids_" name="car_ids[]" value="144" type="checkbox"> Car 144
<input id="car_ids_" name="car_ids[]" value="145" type="checkbox"> Car 145
[@cars[1], @cars[2]].each do |car|
  check("Car #{car.id}")
end
我总是

Capybara::ElementNotFound:
       cannot check field, no checkbox with id, name, or label 'car_ids_:first' found
你能告诉我这是怎么回事吗? 我想选择f.e.第二个和第三个元素

更新:

  find(:css, "#car_ids_[value='#{@car1.id}']").set(true)
  => find(:css, "#car_ids_[value='#{@car1.id}']").checked?
  ==> "checked"
  find(:css, "#car_ids_[value='#{@car2.id}']").set(nil)
  => find(:css, "#car_ids_[value='#{@car1.id}']").checked?
  ==> nil
  find(:css, "#car_ids_[value='#{@car3.id}']").set(true)
  => find(:css, "#car_ids_[value='#{@car1.id}']").checked?
  ==> "checked"

  select("Deactivate", :from => "car_action")

  click_button "Submit"

  page.body.should have_content "Updated cars!"

  @user.cars[0].active?.should == true
  @user.cars[1].active?.should == false
  @user.cars[2].active?.should == true
但最终,复选框值不会传递给控制器!我不知道为什么。。。。
复选框似乎已选中,但如果我使用“保存”和“打开”页面打开页面,它们也不会被选中?

最简单的方法是告诉水豚按标签选中复选框。如果您可以使用@cars集合,您可以执行以下操作:

<input id="car_ids_" name="car_ids[]" value="142" type="checkbox"> Car 142
<input id="car_ids_" name="car_ids[]" value="143" type="checkbox"> Car 143
<input id="car_ids_" name="car_ids[]" value="144" type="checkbox"> Car 144
<input id="car_ids_" name="car_ids[]" value="145" type="checkbox"> Car 145
[@cars[1], @cars[2]].each do |car|
  check("Car #{car.id}")
end
编辑

根据您在下面的评论,如果您想特别访问第2和第3个元素,而不考虑它们所指的集合项目,您可以将输入内容包装在一个带有一些id的div中,以缩小选择器范围,例如,汽车复选框,然后执行以下操作:

find(:css, "#car-checkboxes input:nth-child(2)").click


如果我记得清楚的话:findcar\u ids\uu.first.check这很危险,因为capybara不能保证集合中的元素以它们在标记中出现的相同顺序返回。找到解决方案了吗?是什么让你认为控制器没有得到值?你在params散列中检查过这个吗?是的,params散列中充满了公共信息,但是没有car_id_-array。当我在浏览器中尝试它时,它会工作。。。谢谢你的帮助,但我不能使用标签,因为行中有很多信息,并且我不需要为每个行添加特殊标签…编辑我的答案,看看这是否有帮助。你能看看我的更新吗?复选框现在已选中,但不起作用..:我是个白痴。。我把一些代码弄混了,填完表格后重新加载了页面。。然后我想知道为什么表格是空的。。谢谢