Access session in Helper file ? Rails 3

前端 未结 1 767
北海茫月
北海茫月 2020-12-12 05:12

how to get session in helper file?

UserHelper.rb

module UsersHelper
  def self.auth login, password
    user = Users.where(\"firstna         


        
相关标签:
1条回答
  • 2020-12-12 05:15

    Only Controller can access session.

    So, in a nutshell, if you are going to use this method in Controllers only like what is you case, you can define it as ApplicationController's method. Or define it a module and include it in AppplicationController.

    class ApplicationController < ActionController::Base
      def auth
      end
    
      def is_auth?
      end
    end
    

    If you want to use the method in both controller and view, just declare them as helper_method

    class ApplicationController < ActionController::Base
      helper_method :auth, :is_auth?
      def auth
      end
    
      def is_auth?
      end
    end
    

    Ref: http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method

    Another note: In my opinion it's really not worth the time to build auth system from scratch by yourself. The functionalities are not easy but quite general. There are well baked gems such as Devise, Authlogic. Better to use them.

    0 讨论(0)
提交回复
热议问题