提交表单时无法获取黄瓜显示nested_attributes

时间:2012-07-14 04:14:04

标签: ruby-on-rails cucumber capybara nested-attributes

我正在测试使用黄瓜和嵌套属性上传文档。目前,当我在rails服务器上运行时,它正在上传很好,但是当我尝试用capybaras测试结果时应该方法它失败了。这是我的相关代码

错误消息

Then I should see the job posted and both files attached         # features/step_definitions/job_steps.rb:40
  expected there to be content "test_doc"

功能

Background:
Given I am logged in
And I am on the post a new job page

Scenario: User can post a job with a document and picture attached
  When I post a job with a picture and document attached
  Then I should see the job posted and both files attached

步骤定义

When /^I post a job with a picture and document attached$/ do
  fill_in 'Title', with: "Title"
  fill_in 'Description', with: "Description"
  fill_in 'Value', with: '199.99'
  fill_in 'job_job_documents_attributes_0_name', :with => "test_doc" 
  attach_file('job_job_documents_attributes_0_document', File.join(Rails.root, 'features', 'upload_files', 'test.pdf'))
  fill_in 'job_job_documents_attributes_0_name', with: "test_pic" 
  attach_file('job_job_pictures_attributes_0_picture', File.join(Rails.root, 'features', 'upload_files', 'test.jpg'))
  click_button 'Create Job'

Then /^I should see the job posted and both files attached$/ do
  page.should have_content 'Job Created!'   
  page.should have_content 'test_doc'       <------ FAILING LINE
end

表格

<%= form_for @job, :html => {name: 'job_form'} do |f| %>
  <%= f.label :title %> <br />
  <%= f.text_field :title %>

  <%= f.label :description %> <br /><br />
  <%= f.text_area :description %>

  <%= f.label :value %><br />
  <%= f.text_field :value %>

  <%= f.fields_for :job_documents do |document| %>
    <%= document.label :document %>
    <%= document.file_field :document %>

    <%= document.label :name %>
    <%= document.text_field :name %>
  <% end %>

  <%= f.fields_for :job_pictures do |picture| %>
    <%= picture.label :picture %>
    <%= picture.file_field :picture %>
    <%= picture.label :name %>
    <%= picture.text_field :name %>
  <% end %>
<%= f.submit %>

<% end %>

页面来源

<p>
    <label for="job_job_documents_attributes_0_document">Document</label>
    <input id="job_job_documents_attributes_0_document" name="job[job_documents_attributes][0][document]" type="file" />
  </p>
  <p>
    <label for="job_job_documents_attributes_0_name">Name</label>
    <input id="job_job_documents_attributes_0_name" name="job[job_documents_attributes][0][name]" size="30" type="text" />
  </p>
</p>

所以我的所有非嵌套属性都没有抛出任何错误,但我似乎无法获得水豚看到我的嵌套属性。任何想法?

1 个答案:

答案 0 :(得分:0)

在new方法的控制器中,您需要构建job_documents,就像这样

@job = Job.new
@job.job_documents.build

另外请不要忘记在你的模型中添加attr_accessible job_documents_attributes