Reputation: 71
I have a post model with
accepts_nested_attributes_for :views, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
And a views model with
belongs_to :post, inverse_of: :views
In posts/_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 %>
In posts/_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>
Now in my rspec, I want to fill the f.text_field with some value.
Tried the following:
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"
None of the them work. How do I call fill_in on this field?
Upvotes: 4
Views: 911
Reputation: 3908
While @Stephen's answer is on the right track, namely that you need to specify the id
attribute value of the input field, and it's gonna have some number in it, it does not cover the case when a new nested resource is being added and it does not yet have a nice id. It will tend to get some generated number, for example the HTML may come out like this:
<div class="post_views_content>
<textarea class="text required" name="post[views_attributes][1711119995807][content]" id="post_views_attributes_1711119995807_name"></textarea>
In this case you can still try to find the input element and obtain its id
to pass into fill_in
helper:
input_id = find(".post_views_content textarea")["id"]
fill_in input_id, with: "I support"
Upvotes: 1
Reputation: 187
I'm using a the capybara screenshot gem (Which I highly suggest!). I have a nested form with projects belonging to users. And it accepts
fill_in 'user_projects_attributes_0_name', with: "Name"
So for you, I suggest grabbing the gem for sure. But also try this:
fill_in 'post_views_attributes_0_first_name', with: "I support"
Upvotes: 1