【问题标题】:ActionView::Template::Error: undefined method `to' [closed]ActionView::Template::Error: undefined method `to' [关闭]
【发布时间】:2012-12-31 10:47:39
【问题描述】:

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

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>

谁能帮我解释一下为什么我得到这个未定义的方法错误以及如何摆脱它??

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.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
    

    【讨论】:

    • 我认为你忽略了逗号。
    • 干杯,糟糕的复制粘贴工作;)
    • 不能解决同样的错误!!和它在迈克尔哈特书中(我选择的方法)。在询问之前我检查了 10 次代码并真诚地预订。新年快乐!享受
    • 在要点中发布您的 _header / 应用程序模板
    • @simonmorley 我更新了代码
    【解决方案2】:

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

    【讨论】:

    • 是的,我必须在标题中用 link_to 替换链接,谢谢提示!!
    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多