我有一个帖子模型
accepts_nested_attributes_for :views, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
还有一个视图模型
belongs_to :post, inverse_of: :views
在帖子/_forms.html.erb中
<%= post_form.fields_for :views do |builder| %>
<%= render 'view_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add new View", post_form, :views %>
在帖子/_view_fields.html.erb中
<fieldset>
<%= f.label :name, "View name:" %>
<%= f.text_field :name %>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
现在在我的 rspec 中,我想用一些值填充 f.text_field 。
尝试了以下方法:
fill_in 'post_views_first_name', with: "I support"
fill_in 'post_view_name', with: "I support"
fill_in 'post_views_name', with: "I support"
它们都不起作用。我如何在此字段上调用 fill_in ?
我正在使用水豚截图宝石(我强烈建议!)。我有一个嵌套表单,其中包含属于用户的项目。并且它接受
fill_in 'user_projects_attributes_0_name', with: "Name"
所以对于你来说,我建议一定要抓住宝石。但也可以尝试这个:
fill_in 'post_views_attributes_0_first_name', with: "I support"
虽然@Stephen的答案是正确的,即您需要指定输入字段的
id
属性值,并且其中会有一些数字,但它不涵盖新嵌套资源被创建时的情况添加后,它还没有一个好的 ID。它往往会得到一些生成的数字,例如 HTML 可能会像这样出现:
<div class="post_views_content>
<textarea class="text required" name="post[views_attributes][1711119995807][content]" id="post_views_attributes_1711119995807_name"></textarea>
在这种情况下,您仍然可以尝试找到输入元素并获取其
id
传递给 fill_in
helper:
input_id = find(".post_views_content textarea")["id"]
fill_in input_id, with: "I support"