Ruby on Rail Michael Hartl的第3章错误

时间:2018-03-08 03:52:01

标签: ruby-on-rails ruby

我有这个错误,我一直在迈克尔哈特尔的RoR教程中。是的,我确实谷歌这个问题,看到它回答了几次。我尝试了提供的解决方案,他们没有解决问题。这是错误:

ec2-user:~/environment/sample_app (static-pages) $ rails test
Running via Spring preloader in process 16202
Run options: --seed 53019

# Running:

F

Failure:
StaticPagesControllerTest#test_should_get_about [/home/ec2-


user/environment/sample_app/test/controllers/
static_pages_controller_test.rb:20]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.


bin/rails test test/controllers/static_pages_controller_test.rb:17

F

Failure:
StaticPagesControllerTest#test_should_get_home [/home/ec2-
user/environment/sample_app/test/controllers/
static_pages_controller_test.rb:8]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.


bin/rails test test/controllers/static_pages_controller_test.rb:5

F

Failure:
StaticPagesControllerTest#test_should_get_help [/home/ec2-
user/environment/sample_app/test/controllers/
static_pages_controller_test.rb:14]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.


bin/rails test test/controllers/static_pages_controller_test.rb:11



Finished in 0.171213s, 17.5220 runs/s, 35.0441 assertions/s.
3 runs, 6 assertions, 3 failures, 0 errors, 0 skips

因此,它试图显示的内容以及输入内容存在误解。我明白那一部分。但字面上我的代码读到它应该工作。我完全从书中复制了..

这是我的static_pages_controller_test.rb的代码:

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | Ruby on Rails Tutorial Sample App."
end

test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App."
end

test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App."
end


end

这是我的application.html.erb代码:

<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

所以我很困惑,为什么这不起作用。另一篇文章的一个答案表明它与帮助没有html的东西有关..但我的确如此。任何人都能看到这里发生了什么。我使用的是cloud9 IDE。

谢谢, 本

编辑1:更新

这是我的家,帮助和html代码。主页:

<% provide(:title, "Home") %>

<h1>Sample App</h1>

<p>This is the home page for the
<a href="http://www.railstutorial.org/">Ruby on Rails</a>
sample application
</p>

帮助:

<% provide(:title, "Help") %>
<h1>Help</h1>

<p>
Get help on the Ruby on Rails Tutorial at the
<a href="http://www.railstutorial.org/help">Rails Tutorial help section</a>.
To get help on this sample app, see the
<a href="http://www.railstutorial.org/book"><em>Ruby on Rails Tutorial</em>
book</a>.
</p>

关于:

<% provide(:title, "About") %>

<h1>About</h1>

<p>
The <a href="http://www.railstutorial.org/"><em>Ruby on Rails
Tutorial</em></a> is a
<a href="http://www.railstutorial.org/book">book</a> and
<a href="http://screencasts.railstutorial.org/">screencast series</a>
to teach web development with
<a href="http://rubyonrails.org/">Ruby on Rails</a>.
This is the sample application for the tutorial.
</p>

要回答以下问题,此代码尚未推送到回购。在我发布之前,我在本地托管(并测试它)。当我在本地浏览器中查看它时,页面可以正常工作,但标题(浏览器顶部的选项卡)未填充。所以很明显标题功能不起作用。

Upadate 2:添加routes.rb

Rails.application.routes.draw do

root 'static_pages#home'
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'

# For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html

# root 'application#hello'

end

1 个答案:

答案 0 :(得分:0)

在学习Rails时,我也遇到了同样的问题,但是我在另一篇StackOverflow文章中找到了答案。

  

provide将一个标记块存储在一个标识符中,以备后用。在这种情况下,请在符号:title中使用“帮助”。报价包含在<% %>中,表示它正在执行此代码,并且没有在视图中打印出来。

     

yield在这种情况下只是吐出而被阻止。收益率包含在<%= %>中,以表明收益率已被打印到视图中。

     

将其视为设置变量并打印出变量。

     

有关更多信息,请参见:http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-provide。请注意,provide实际上是content_for的包装,因此,该链接中的好地方就在这里。

这摘自以下StackOverflow文章:yield and provide() inside template