Rails 3辅助方法在模型中可用

use*_*808 10 ruby-on-rails ruby-on-rails-3

我正在将应用程序升级到Rails 3.我们有一个类似以下的应用程序控制器:

class ApplicationController < ActionController::Base
     helper_method :current_user

     def current_user
       @current_user ||= User.find(session[:user_id]) if session[:user_id]
     end
Run Code Online (Sandbox Code Playgroud)

因为我们定义了"helper_method",所以所有视图都可以使用"current_user".它可供控制器使用,因为它们都继承自ApplicationController类.

但是,当我们使用2.3.8时,通过模型可以访问"current_user",但现在却没有.有没有办法让这个暴露在模型中?

Kel*_*lly 9

我可以建议搬进current_user一个帮手UserHelper,然后你可以把它包含在你的模型中include UserHelper.

您还必须include UserHelper在ApplicationController中,但 helper :user也会将其包含在您的视图中.