当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – Ruby on Rails中未定义的局部变量/无方法错误

来源:互联网 收集:自由互联 发布时间:2021-06-23
任何人都可以向我解释为什么我收到此错误? Rails是关于约定的.有没有更传统的做法,我正在尝试做下面的事情? undefined local variable or method `hello_world' for ##Class:...:... 这是我的文件:
任何人都可以向我解释为什么我收到此错误? 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_world controller method available to the view.

网友评论