StaticPages #home中的NameError

时间:2016-09-13 01:30:17

标签: mysql ruby-on-rails ubuntu passenger production

我得到了#34; NameError"在routes.rb中执行以下操作后

Rails.application.routes.draw do
  root 'static_pages#home'
  get '/help',    to: 'static_pages/help'
  get '/about',   to: 'static_pages/about'
  get '/contact', to: 'static_pages/contact'
  get '/sigunup', to: 'users#new'

end

有人可以帮帮我吗?感谢。

Showing /home/bdme551/bdme21/app/views/layouts/_header.html.erb where line #7 raised:

undefined local variable or method `help_path' for #<#<Class:0x007f7474f3d208>:0x007f747555c508>
Extracted source (around line #7):


      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home",   root_path %></li>
        <li><%= link_to "Help",   help_path %></li>
        <li><%= link_to "Log in", '#' %></li>
      </ul>
    </nav>

StaticPagesController具有以下内容:

class StaticPagesController < ApplicationController
  def home
  end
  def help
  end
  def about
  end
  def contact
  end
end

1 个答案:

答案 0 :(得分:1)

问题是您尚未为自定义GET路由定义名称。如果您运行rake routes,则除prefix之外的任何路线都不应找到root

您可以使用as选项为自定义路线添加名称。

get '/help',    to: 'static_pages#help', as: :help

请注意,在您的代码中,static_pages/help选项的值为to

应该是static_pages#help#指的是实例方法。

现在,如果您尝试运行rake routes,则应该找到help作为/help路由的前缀。

另外,通过将/替换为#来修复其他路径定义。