The html snippet:
<table id="project_1">
<thead>
...
<tr>
<form accept-charset="UTF-8" action="/projects/1/tasks" class="add-task form-search"
data-remote="true" id="new_task" method="post"></form>
<th><input class="input-xlarge" id="task_content" name="task[content]"
placeholder="Start typing here to create a task..." size="50" type="text"></th>
<th><input class="btn" name="commit" type="submit" value="Add Task"></th>
</tr>
</thead>
...
</table>
The rspec test with capybara:
it "should create a task", :js => true do
visit projects_path
within("#project_#{@project.id}") do
fill_in "task[content]", :with => "This is a new task"
save_and_open_page
click_button 'Add Task'
end
page.should have_content("This is a new task")
end
Capybara successfully found all elements on a page, but in the page (generated by save_an_open_page) the field ‘task[content]’ is empty.
What might be a problem here? Thanks
UPDATE:
The html code generated from this erb:
...
<%= form_for [project, Task.new], html: { :class => "add-task form-search" }, remote: true do |f| %>
<th><%= f.text_field :content, :class => "input-xlarge", :placeholder => "Start typing here to create a task...", :size => 50 %></th>
<th><%= f.submit "Add Task", :class => "btn" %></th>
<% end %>
...
UPDATE:
The closed tag (noticed by @Steve) may indeed be your problem. Here is someone with the same problem: form_for closes <form> tag
I suspect the problem may be your HTML tags in the form. What if you remove the table row/header tags, like this:
See if that generates the correct HTML first (with the
<form>tags wrapping the<input>tags, and then see if that makes the spec pass.ORIGINAL ANSWER:
I suspect that the problem is not with
fill_in, but with what happens after it (adding the task). In my own test, I found thatsave_and_open_pagedoes not show the text in the actual field even when the test actually does work. Are you sure there is not a problem with adding the new task to the page after form submission?Other things to try:
withinblock and see if it works.idinstead of itsnameinfill_in, i.e.:fill_in "task_content", :with => "This is a new task"'and see if that works.