使用application_controller或helper文件的auth帮助器方法

时间:2013-09-25 19:04:40

标签: ruby-on-rails authentication ruby-on-rails-4

我想从头开始创建我的身份验证系统,我并不是真的在这方面寻找辩论。我的问题是我应该使用帮助文件来提供诸如current_user和signed_in之类的方法吗?或者是否更喜欢通过application_controller提供这些?或者这无关紧要,我只需要继续下去?

1 个答案:

答案 0 :(得分:0)

我会把它放在ApplicationController中,你可以定义像

这样的东西
def current_user 
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

这将允许您从视图和控制器中调用@current_user。

相关问题