Rails:控制器中包含的模块中的方法在视图中不可用

发布于 2024-07-30 08:44:19 字数 254 浏览 3 评论 0原文

奇怪的事情 - 我在 lib/ 中有身份验证模块,如下所示:

module Authentication
  protected

  def current_user
    User.find(1)
  end

end

在 ApplicationController 中,我包含此模块和所有帮助程序,但方法 current_user 在控制器中可用,但在视图中不可用:( 我怎样才能让这个工作?

Strange thing – I have Authentication module in lib/ like this:

module Authentication
  protected

  def current_user
    User.find(1)
  end

end

and in ApplicationController I'm including this module and all helpers, but method current_user is available in controllers, but not from views :( How can I make this work?

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

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

发布评论

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

评论(2

∞琼窗梦回ˉ 2024-08-06 08:44:19

如果该方法是直接在控制器中定义的,则必须通过调用 helper_method :method_name 使其可用于视图。

class ApplicationController < ActionController::Base

  def current_user
    # ...
  end

  helper_method :current_user
end

使用模块,您可以执行相同的操作,但有点棘手。

module Authentication
  def current_user
    # ...
  end

  def self.included m
    return unless m < ActionController::Base
    m.helper_method :current_user # , :any_other_helper_methods
  end
end

class ApplicationController < ActionController::Base
  include Authentication
end

啊,是的,如果你的模块严格来说是一个辅助模块,你可以按照 Lichtamberg 所说的那样做。 但话又说回来,您可以将其命名为 AuthenticationHelper 并将其放入 app/helpers 文件夹中。

不过,根据我自己使用身份验证代码的经验,您将希望控制器和视图都可以使用它。 因为通常您将在控制器中处理授权。 助手仅供视图使用。 (我相信它们最初是作为复杂 html 结构的简写。)

If the method were defined directly in the controller, you'd have to make it available to views by calling helper_method :method_name.

class ApplicationController < ActionController::Base

  def current_user
    # ...
  end

  helper_method :current_user
end

With a module, you can do the same, but it's a bit more tricky.

module Authentication
  def current_user
    # ...
  end

  def self.included m
    return unless m < ActionController::Base
    m.helper_method :current_user # , :any_other_helper_methods
  end
end

class ApplicationController < ActionController::Base
  include Authentication
end

Ah, yes, if your module is meant to be strictly a helper module, you can do as Lichtamberg said. But then again, you could just name it AuthenticationHelper and put it in the app/helpers folder.

Although, by my own experience with authentication code, you will want to have it be available to both the controller and views. Because generally you'll handle authorization in the controller. Helpers are exclusively available to the view. (I believe them to be originally intended as shorthands for complex html constructs.)

哆兒滾 2024-08-06 08:44:19

声明了它

  helper :foo             # => requires 'foo_helper' and includes FooHelper
  helper 'resources/foo'  # => requires 'resources/foo_helper' and includes Resources::FooHelper

您是否在 ApplicationController 中

http://railsapi.com/ doc/rails-v2.3.3.1/classes/ActionController/Helpers/ClassMethods.html#M001904

Did you declare it with

  helper :foo             # => requires 'foo_helper' and includes FooHelper
  helper 'resources/foo'  # => requires 'resources/foo_helper' and includes Resources::FooHelper

in your ApplicationController?

http://railsapi.com/doc/rails-v2.3.3.1/classes/ActionController/Helpers/ClassMethods.html#M001904

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