gpt4 book ai didi

ruby-on-rails - 为什么我的 haml 页面在源代码 View 中损坏了?

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:24 24 4
gpt4 key购买 nike

当我运行我的测试时,它失败了,如下所示:测试报错信息:

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'

从查看源代码页面可以看出,结束标记位于打开标记旁边的顶部,而不是页面末尾。这就是 capybara 提示的原因。

<!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>

我在这里遗漏了什么吗?

最佳答案

一切都应该在 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

关于ruby-on-rails - 为什么我的 haml 页面在源代码 View 中损坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18883805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com