2

当我运行我的测试时,它失败如下:测试错误消息:

Failure/Error: page.should have_content 'Email'
     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching xpath "/html"

我在网上搜索了如何构造haml文件,我认为它是正确的。

这是我的 application.html.haml

!!! 5
%html
%head
  %title Hello
  = stylesheet_link_tag    'application', media: 'all'
  = javascript_include_tag 'application'
  = csrf_meta_tags

%body
  = yield

这里是:new.html.haml

%h1 Sign Up
= render partial: 'form'

这是我的部分形式:_form.html.haml

= form_for @user do |f|
  - if @user.errors.any?
    %div.error_messages
      %h2 Form is invalid
      %ul
        - @user.errors.full_messages.each do |message|
          %li= message
  = f.label :full_name, 'Full Name:'
  = f.text_field :full_name
  %br

  = f.label :email, 'Email:'
  = f.text_field :email
  %br

  = f.label :password, 'Password:'
  = f.text_field :password
  %br

  = f.label :password_confirmation, 'Confirm Password:'
  = f.text_field :password_confirmation
  %br

  = f.submit 'Sign Up'

从查看源页面可以看出,结束标记位于打开标记旁边的顶部,而不是页面末尾。这就是水豚抱怨的原因。

<!DOCTYPE html>
<html></html>
<head>
  <title>Hello</title>
  <link href="/assets/application.css?body=1" media="all" rel="stylesheet" />
  <script src="/assets/jquery.js?body=1"></script>
  <script src="/assets/jquery_ujs.js?body=1"></script>
  <script src="/assets/turbolinks.js?body=1"></script>
  <script src="/assets/application.js?body=1"></script>
</head>
<body>
  <h1>Sign Up</h1>
  <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="n6sYrcT+Z7LtaH1AwT6h5/knsdMFRzwDTXAWZh6VyfI=" /></div>  <label for="user_full_name">Full Name:</label>
    <input id="user_full_name" name="user[full_name]" type="text" />
    <br>
    <label for="user_email">Email:</label>
    <input id="user_email" name="user[email]" type="text" />
    <br>
    <label for="user_password">Password:</label>
    <input id="user_password" name="user[password]" type="text" />
    <br>
    <label for="user_password_confirmation">Confirm Password:</label>
    <input id="user_password_confirmation" name="user[password_confirmation]" type="text" />
    <br>
    <input name="commit" type="submit" value="Sign Up" />
  </form>
</body>

我在这里错过了什么吗?

4

2 回答 2

3

一切都应该在 HAML html 标记之后缩进。这个缩进让 HAML 知道如何嵌套你的标签。因此,application.html.haml 应该是:

!!! 5
%html
  %head
    %title Hello
    = stylesheet_link_tag    'application', media: 'all'
    = javascript_include_tag 'application'
    = csrf_meta_tags

  %body
    = yield
于 2013-09-18T23:17:46.017 回答
2

您需要缩进html标签的所有内容:

!!! 5
%html
  %head
    %title Hello
    = stylesheet_link_tag    'application', media: 'all'
    = javascript_include_tag 'application'
    = csrf_meta_tags

  %body
    = yield

你在做什么是不同的:

!!! 5
%html
%head
  ...

这告诉haml 渲染一个html没有嵌入内容的head标签,然后是一个嵌入了内容的标签。缩进html标签内的所有内容可以解决此问题。

于 2013-09-18T23:14:27.090 回答