ActionView :: Template ::错误:未定义的方法`到'

时间:2012-12-31 10:47:40

标签: ruby-on-rails ruby-on-rails-3.2

我在测试错误的应用日志时遇到错误

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>

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你可能在_header文件中输入了一个拼写错误,因为每个错误都有它,再次检查_header文件,如果你运行rails服务器并尝试访问这些页面,你应该得到相同的错误。