【问题标题】:undefined local variable or method 'faq'未定义的局部变量或方法“常见问题解答”
【发布时间】:2013-12-31 01:35:24
【问题描述】:

每当我运行http://localhost:3000 时,我都会得到:

undefined local variable or method `faq_path' for #<#<Class:0x007fb238145c20>:0x007fb235054b98>
Extracted source (around line #12):

   <body>
   <%= link_to "Home", root_path %>
   <%= link_to "About", about_path %>
   <%= link_to "Faq", faq_path %>
   <%= yield %>
   </body>
   </html>

我的路线文件

Document::Application.routes.draw do
  root "pages#home"
  get "about" => "pages#about"
  get "faq" => "pages#faq"

我的页面控制器文件

class PagesController < ApplicationController
  def home
  end

  def about
  end

  def faq
  end
end

还有我的布局文件

<!DOCTYPE html>
<html>
<head>
  <title>Document</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= link_to "Home", root_path %>
<%= link_to "About", about_path %>
<%= link_to "Faq", faq_path %>
<%= yield %>
</body>
</html>

这是我运行rake routes 时得到的结果 前缀动词 URI 模式控制器#Action 根 GET / pages#home 关于 GET /about(.:format) pages#about 常见问题 GET /faq(.:format) pages#faq

【问题讨论】:

  • 你能告诉我们你的路线清单吗?只需在控制台输入rake routes 或更好的bundle exec rake routes

标签: ruby-on-rails


【解决方案1】:

尝试将路由更新为get "faq" =&gt; "pages#faq", :as =&gt; :faq

除非您已在 routes.rb 的其他位置定义它,否则您可能会在 about_path 中看到相同的问题。

【讨论】:

    【解决方案2】:

    尝试在routes.rb 文件中使用match,如下所示:

    match '/faq', :to =&gt; 'pages#faq'

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 1970-01-01
      • 2018-04-19
      • 2015-01-01
      • 2013-12-17
      • 2016-04-29
      相关资源
      最近更新 更多