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

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

概述任何人都可以向我解释为什么我收到此错误? Rails是关于约定的.有没有更传统的做法,我正在尝试做下面的事情? undefined local variable or method `hello_world' for #<#<Class:...>:...> 这是我的文件: welcome_controller.rb class WelcomeController < ApplicationCont 任何人都可以向我解释为什么我收到此错误? Rails是关于约定的.有没有更传统的做法,我正在尝试做下面的事情?

undefined local variable or method `hello_world' for #<#<Class:...>:...>

这是我的文件:

welcome_controller.rb

class WelcomeController < ApplicationController  def hello_world    "Hello,World"  endend

欢迎/ 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" endend

现在在视图中调用它:

<%= hello_world %>

阅读helper_method

Declare a controller method as a helper,to make the hello_world controller method available to the vIEw.

总结

以上是内存溢出为你收集整理的ruby-on-rails – Ruby on Rails中未定义局部变量/无方法错误全部内容,希望文章能够帮你解决ruby-on-rails – Ruby on Rails中未定义的局部变量/无方法错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1242003.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存