Ruby on Rails - Access controller variable from model

后端 未结 5 958
深忆病人
深忆病人 2020-11-30 06:12

I am trying to access an instance variable which is set in the controller in the model. The controller is the products controller and the model is the products model. The in

5条回答
  •  失恋的感觉
    2020-11-30 06:23

    You shouldn't generally try to access the controller from the model for high-minded issues I won't go into.

    I solved a similar problem like so:

    class Account < ActiveRecord::Base
      cattr_accessor :current
    end
    
    class ApplicationController < ActionController::Base
      before_filter :set_current_account
      def set_current_account
        #  set @current_account from session data here
        Account.current = @current_account
      end
    end
    

    Then just access the current account with Account.current

提交回复
热议问题