Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么我的haml页面在源代码视图中被破坏?_Ruby On Rails_Ruby_Testing_Capybara - Fatal编程技术网

Ruby on rails 为什么我的haml页面在源代码视图中被破坏?

Ruby on rails 为什么我的haml页面在源代码视图中被破坏?,ruby-on-rails,ruby,testing,capybara,Ruby On Rails,Ruby,Testing,Capybara,当我运行测试时,失败如下: 测试错误消息: 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 =

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

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
%h1 Sign Up
= render partial: 'form'
= 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'
下面是:new.html.haml

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

%body
  = yield
%h1 Sign Up
= render partial: 'form'
= 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'
这是我的部分表单:_form.html.haml

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

%body
  = yield
%h1 Sign Up
= render partial: 'form'
= 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>

你好
注册
全名:

电邮:
密码:
确认密码:

我在这里遗漏了什么吗?

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

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

  %body
    = yield
!!! 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
标记可以解决此问题。

所有内容都应该缩进到HAML html标记之后。这个缩进让HAML知道如何嵌套标签。因此,application.html.haml应该是: