Ruby on Rails中未定义的局部变量无方法错误,如何解决这个难题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计282个文字,预计阅读时间需要2分钟。
为什么我收到了这个错误?Rails是关于约定的,有没有更传统的做法?我正在尝试做下面的事情:undefined local variable or method `hello_world'
这可能是因为你在Rails中尝试使用一个未定义的局部变量或方法。确保在调用`hello_world`之前已经定义了它,无论是作为一个变量还是作为方法。
任何人都可以向我解释为什么我收到此错误? Rails是关于约定的.有没有更传统的做法,我正在尝试做下面的事情?undefined local variable or method `hello_world' for #<#<Class:...>:...>
这是我的文件:
welcome_controller.rb
class WelcomeController < ApplicationController def hello_world "Hello, World" end end
欢迎/ index.html.erb
<%= hello_world %>
的routes.rb
Rails.application.routes.draw do get 'welcome/index' root 'welcome#index' end 或者做:
class WelcomeController < ApplicationController helper_method :hello_world def hello_world "Hello, World" end end
现在在视图中调用它:
<%= hello_world %>
阅读helper_method
Declare a controller method as a helper, to make the
hello_worldcontroller method available to the view.
本文共计282个文字,预计阅读时间需要2分钟。
为什么我收到了这个错误?Rails是关于约定的,有没有更传统的做法?我正在尝试做下面的事情:undefined local variable or method `hello_world'
这可能是因为你在Rails中尝试使用一个未定义的局部变量或方法。确保在调用`hello_world`之前已经定义了它,无论是作为一个变量还是作为方法。
任何人都可以向我解释为什么我收到此错误? Rails是关于约定的.有没有更传统的做法,我正在尝试做下面的事情?undefined local variable or method `hello_world' for #<#<Class:...>:...>
这是我的文件:
welcome_controller.rb
class WelcomeController < ApplicationController def hello_world "Hello, World" end end
欢迎/ index.html.erb
<%= hello_world %>
的routes.rb
Rails.application.routes.draw do get 'welcome/index' root 'welcome#index' end 或者做:
class WelcomeController < ApplicationController helper_method :hello_world def hello_world "Hello, World" end end
现在在视图中调用它:
<%= hello_world %>
阅读helper_method
Declare a controller method as a helper, to make the
hello_worldcontroller method available to the view.

