2016-04-26 74 views
0

下面是我的形式,你可以看到,会有输入领域的许多行都具有相同的类:水豚FILL_IN多个输入字段

<%= q.simple_fields_for :choices do |c|%> 
    <div class="row-fluid choice-row"> 
    <%= c.text_field :sort_order, class: 'span1' %> 
    <%= c.text_field :title, class: 'span9' %> 
    <%= c.link_to_remove 'Remove', class: 'btn btn-danger span2 pull-right' %> 
    </div> 
<% end %> 

对于我的测试,我想:

fill_in(:sort_order, with: '3') 
fill_in(:title, with: 'Choice 3') 

我也试过:

fill_in('Sort Order', with: '3') 
fill_in('Title', with: 'Choice 3') 

和:

fill_in('Sort Order', :with => '3') 
fill_in('Title', :with => 'Choice 3') 

我得到的错误是:

Failure/Error: fill_in(:title, with: 'Choice 3') 
Capybara::ElementNotFound: 
    cannot fill in, no text field, text area or password field with id,   name, or label 'title' found 

是否有人可以推荐我怎么能填充特定的输入,好比说第三排?

+0

使用字段的ID而不是'Title'。 – psantos

+0

我还没有分配一个ID。即使我这样做,也不会每个输入都有相同的ID。我如何区分第三行输入和第五行? –

+0

每个字段的ID是唯一的。你可以检查你的字段,看看哪些ID号分配给它 – psantos

回答

0

你会发现在你的标记,通过形式辅助域输出传递到fields_for对象中的命名空间,使用fields_for这的确是整点

在这种情况下,您的表单元素将具有名称,如choices[title]choices[sort_order]

要找到并fill_in按名称选择时,正确的领域产生了很多领域,你需要获得更多的创意,因为这不是一个很典型的设置:

  • 您可以使用within选择一个独特的父元素
  • 您可以使用CSS选择器找到nth元素
  • 你可以找到的所有元素,并使用[2]返回数组访问第3元素