how to pass session variable to model in RoR?

前端 未结 4 1186
时光取名叫无心
时光取名叫无心 2021-01-13 07:06

I used a global variable in my app for passing information before. But I got a problem and thanks everyone here suggested me to store those data in session with database.

4条回答
  •  难免孤独
    2021-01-13 07:26

    Best way of doing this is to Create a method with an argument and pass session as an argument.

    Fro example

    class User < ActiveRecord::Base
    
      def self.some_method(some_arg)
         User.find(some_arg)
      end
    
    end
    

    from controller

    User.some_method(session[:user_id])
    

提交回复
热议问题