【问题标题】:undefined local variable or method `welcome_goodbye_path' in Rails?Rails 中未定义的局部变量或方法“welcome_goodbye_path”?
【发布时间】:2015-06-19 01:47:03
【问题描述】:

我是 Rails 的初学者,正在尝试学习“使用 Rails 进行敏捷 Web 开发”。我想创建一个网页链接。这是我的代码:

<h1>Hello, Rails!</h1>
<p>
It is now <%= Time.now %>
</p>
<p>
Time to say
<%= link_to "Goodbye", welcome_goodbye_path %>!
</p>

但这会出错...

undefined local variable or method `welcome_goodbye_path' 

我做错了什么? 这是我的欢迎控制器的代码:

class WelcomeController < ApplicationController
  def index
  end
  def goodbye
  end
end

这是 rake 路由的结果:

        Prefix Verb   URI Pattern                  Controller#Action
     articles GET    /articles(.:format)          articles#index
              POST   /articles(.:format)          articles#create
  new_article GET    /articles/new(.:format)      articles#new
 edit_article GET    /articles/:id/edit(.:format) articles#edit
      article GET    /articles/:id(.:format)      articles#show
              PATCH  /articles/:id(.:format)      articles#update
              PUT    /articles/:id(.:format)      articles#update
              DELETE /articles/:id(.:format)      articles#destroy
welcome_index GET    /welcome(.:format)           welcome#index
              POST   /welcome(.:format)           welcome#create
  new_welcome GET    /welcome/new(.:format)       welcome#new
 edit_welcome GET    /welcome/:id/edit(.:format)  welcome#edit
      welcome GET    /welcome/:id(.:format)       welcome#show
              PATCH  /welcome/:id(.:format)       welcome#update
              PUT    /welcome/:id(.:format)       welcome#update
              DELETE /welcome/:id(.:format)       welcome#destroy
         root GET    /   

【问题讨论】:

    标签: ruby-on-rails ruby hyperlink error-handling


    【解决方案1】:

    您必须显示config/routes.rb。你需要一些东西,比如:

    get 'goodbye', to: 'welcome#goodbye', as: 'welcome_goodbye'
    

    【讨论】:

    • 非常感谢!!它解决了我的问题......如果你能提供一些信息来说明我为什么需要这样做......那就太好了!
    • 阅读this guide,你就会知道你需要知道的一切。 ?(阅读所有这些指南......如果你问我,它们比大多数书都好)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多