视图调用方法时未定义的局部变量或方法

发布于 2024-11-14 10:42:26 字数 531 浏览 4 评论 0原文

我正在尝试向我的用户发送“欢迎消息”:

 #welcome_controller.rb
    class WelcomeController < ApplicationController
      def hi
        @current_user
        if (@current_user)
          @welr = '¡Bienvenido' + current_user + ' a nuestra web!' 
        else
          @weli = "¡Bienvenido invitado, no dude en registrarse!"
        end
      end
    end
 #hi.html.erb Only the call
    <%= hi %>

当我初始化服务器时,控制器给我以下消息:

未定义的局部变量或方法“hi”

我已经尝试了很多修复此问题的方法,但我不能。

I'm trying give a "Welcome Message" to my users with that:

 #welcome_controller.rb
    class WelcomeController < ApplicationController
      def hi
        @current_user
        if (@current_user)
          @welr = '¡Bienvenido' + current_user + ' a nuestra web!' 
        else
          @weli = "¡Bienvenido invitado, no dude en registrarse!"
        end
      end
    end
 #hi.html.erb Only the call
    <%= hi %>

When I initialize my server the controller give me this message:

undefined local variable or method `hi' for

I have tried many wways of repairing this but I can't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-11-21 10:42:26

您需要在控制器中将 hi 定义为 helper_method 。 这样的东西

class WelcomeController < ApplicationController
  helper_method :hi

  def hi
    # your stuff here...
  end

像end

请参阅 http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper_method 了解更多信息

You need to define hi as a helper_method in your controller. Something like

class WelcomeController < ApplicationController
  helper_method :hi

  def hi
    # your stuff here...
  end

end

See http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper_method for more info

不顾 2024-11-21 10:42:26

这不是您使用控制器方法的方式。在 Rails 中,控制器上定义的方法用于“设置”特定视图所需的数据,或处理给定的请求。它们不应该由视图直接调用。

对于您想要执行的操作,您需要向 WelcomeHelper 添加一个辅助方法。因此,假设您希望 http://yourapp.dev/welcome/ 输出上面的消息,这就是您需要的:

# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
  def index
    # Explicitly defining the `index` method is somewhat redundant, given
    # that you appear to have no other logic for this view. However, I have
    # included it for the sake of example.
  end
end    

# app/views/welcome/index.html.erb
<%= greeting %>

# app/helpers/welcome_helper.rb
class WelcomeHelper
  # All methods in WelcomeHelper will be made available to any views
  # that are part of WelcomeController.
  def welcome
    if (@current_user)
      # You may need to change this to something like `@current_user.name`,
      # depending on what @current_user actually is.
      '¡Bienvenido' + @current_user + ' a nuestra web!' 
    else
      "¡Bienvenido invitado, no dude en registrarse!"
    end
  end
end

That's not how you use controller methods. In Rails, methods defined on a controller are used to 'set up' the data needed for a particular view, or to handle a given request. They're not supposed to be called directly by a view.

For what you're trying to do, you need to add a helper method to WelcomeHelper. So, assuming you want http://yourapp.dev/welcome/ to output the message above, this is what you'd need:

# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
  def index
    # Explicitly defining the `index` method is somewhat redundant, given
    # that you appear to have no other logic for this view. However, I have
    # included it for the sake of example.
  end
end    

# app/views/welcome/index.html.erb
<%= greeting %>

# app/helpers/welcome_helper.rb
class WelcomeHelper
  # All methods in WelcomeHelper will be made available to any views
  # that are part of WelcomeController.
  def welcome
    if (@current_user)
      # You may need to change this to something like `@current_user.name`,
      # depending on what @current_user actually is.
      '¡Bienvenido' + @current_user + ' a nuestra web!' 
    else
      "¡Bienvenido invitado, no dude en registrarse!"
    end
  end
end
坦然微笑 2024-11-21 10:42:26

这篇文章可能对您有帮助:
Ruby on Rails:从您的视图访问控制器方法

只需写:

<% @controller.hi %>

This article may help you :
Ruby on Rails: Accessing Controller Methods from Your View

Just write:

<% @controller.hi %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文