how to pass session variable to model in RoR?

前端 未结 4 1157
时光取名叫无心
时光取名叫无心 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])
    
    0 讨论(0)
  • 2021-01-13 07:27

    Do you need the session variable as part of your model, or just as a flag to determine what to execute?

    If the former, you don't need to know where did the parameters originate, just pass the variable as an argument for some call in your method. For example:

    @user = User.new(params[:user].merge(:some_attribute => session[:some_key])

    and just add the validation as usual in the model.

    If you need that to control some flow of the execution, as you mention that should be different for different users, you may need an instance variable in your controller. Something like

    class SomeController
      before_filter :set_some_session_variable
    
      def set_some_session_variable
        @some_variable = session[:some_key]
      end
    end
    

    You could use session[:some_key] directly in your view, but is better to set it in an instance variable instead.

    0 讨论(0)
  • 2021-01-13 07:28

    Models are an interface to the database. They can be used without a session (e.g. from IRB). It would be a violation of MVC to allow the models to talk to the session. Can you expand your question with a bit more info about what you're trying to do? There is most probably a better way to do it.

    0 讨论(0)
  • 2021-01-13 07:32

    If I understand you correctly, a session variable changes the way you validate the model. I believe the correct solution for this is the following:

    class Blog < ActiveRecord::Base
      attr_accessor :validate_title
    
      validate_presence_of :title, :if => :validate_title
    end
    
    class BlogsController < ApplicationController
      def new
        @blog = Blog.new
        @blog.validate_title = session[:validate_title]
      end
    end
    

    The code has not been testet, but that's the idea. The if argument can be the name of a method and you can do whatever you want in there. You can have various validation modes if you want. For example:

    class Blog < ActiveRecord::Base
      attr_accessor :validation_mode
    
      validate_presence_of :title, :if => :validate_title
    
      def validate_title
        validation_mode == "full" or validation_mode == "only_title"
      end
    end
    
    class BlogsController < ApplicationController
      def new
        @blog = Blog.new
        @blog.validate_mode = session[:validate_mode]
      end
    end
    

    For more information, read the guide on validation.

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