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

时间:2021-08-13 23:22:47

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

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

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?

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

2 个解决方案

#1


30  

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

如果方法是直接在控制器中定义的,则必须通过调用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.

啊,是的,如果你的模块是严格意义上的帮助模块,你可以像Lichtamberg所说的那样做。但话说回来,你可以将它命名为AuthenticationHelper并将其放在app / helpers文件夹中。

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.)

虽然,根据我自己的身份验证代码经验,您可能希望控制器和视图都可以使用它。因为通常你会在控制器中处理授权。帮助者可以独家观看。 (我相信它们最初是作为复杂html构造的缩写。)

#2


1  

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?

在你的ApplicationController中?

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

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

#1


30  

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

如果方法是直接在控制器中定义的,则必须通过调用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.

啊,是的,如果你的模块是严格意义上的帮助模块,你可以像Lichtamberg所说的那样做。但话说回来,你可以将它命名为AuthenticationHelper并将其放在app / helpers文件夹中。

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.)

虽然,根据我自己的身份验证代码经验,您可能希望控制器和视图都可以使用它。因为通常你会在控制器中处理授权。帮助者可以独家观看。 (我相信它们最初是作为复杂html构造的缩写。)

#2


1  

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?

在你的ApplicationController中?

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

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