我有很多这样的行:
<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
我想要这种行为,因为我在这个 railscast 中看到了它: http: //railscasts.com/episodes/165-edit-multiple?view=asciicast
当我测试自己时,它可以工作,但是当我尝试使用 Capybara 选择一些元素时,我不知道如何?
before(:each) do
check "car_ids_:first"
check "car_ids_:last"
end
我总是
Capybara::ElementNotFound:
cannot check field, no checkbox with id, name, or label 'car_ids_:first' found
你能帮我看看这是怎么回事吗?我想选择 fe 的第二个和第三个元素?
更新:
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
但最后,复选框值没有传递给控制器!我不知道为什么....复选框似乎已选中(但如果我用 save_and_open_page 打开页面,它们也没有被选中?)