1

当我测试我的应用程序错误日志时出现错误

1) Static pages Home page should have the h1 'Sample App'
     Failure/Error: visit root_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xa674fa8>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:5:in `block (3 levels) in <top (required)>'

  2) Static pages Home page should have the base title
     Failure/Error: visit root_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xa8eaf58>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

  3) Static pages Home page should not have a custom page title
     Failure/Error: visit root_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xb63d804>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top (required)>'

  4) Static pages Help page should have the h1 'Help'
     Failure/Error: visit help_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xa65f284>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>'

  5) Static pages Help page should have the title 'Help'
     Failure/Error: visit help_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xa56dcb8>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  6) Static pages About page should have the h1 'About'
     Failure/Error: visit about_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xb839450>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in <top (required)>'

  7) Static pages About page should have the title 'About Us'
     Failure/Error: visit about_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xa65a464>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>'

  8) Static pages Contact page should have the h1 'Contact'
     Failure/Error: visit contact_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xa56d808>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:40:in `block (3 levels) in <top (required)>'

  9) Static pages Contact page should have the title 'Contact'
     Failure/Error: visit contact_path
     ActionView::Template::Error:
       undefined method `to' for #<#<Class:0xa66470c>:0xb835be8>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___446387253_86947970'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb__887388821_90453380'
     # ./spec/requests/static_pages_spec.rb:44:in `block (3 levels) in <top (required)>'

Finished in 0.45434 seconds
9 examples, 9 failures

Failed examples:

我的 routes.rb 文件是

Sample::Application.routes.draw do
 root to: 'static_pages#home'

match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
match '/about' , to: 'static_pages#about'
  end

我的 _header.html.erb 代码是

<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link to "sample app", '#', id: "logo" %>

<nav>
<ul class="nav pull-right">
<li><%= link_to "Home",  '#' %></li>
<li><%= link_to "Help", '#' %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</div>
</div>
</header>

和 application.html.erb 代码是

<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>

任何人都可以请帮助我为什么得到这个未定义的方法错误以及如何摆脱它?

4

2 回答 2

1

试试这个:

Sample::Application.routes.draw do
   match '/help' => 'static_pages#help'
   match '/contact' => 'static_pages#contact'
   match '/about' => 'static_pages#about'
   root :to => 'static_pages#home'
end
于 2012-12-31T10:56:43.793 回答
0

可能你在 _header 文件中有错字,因为每个错误都有它,再次检查 _header 文件,如果是这种情况,你应该在运行 rails server 并尝试访问页面时遇到同样的错误。

于 2012-12-31T11:25:22.990 回答