我在ApplicationController中聲明瞭一個全局變量,但是我試圖從任何模型中訪問它,但是當這樣做的時候變量在模型中是空的,因爲我在AplicationController中分配了一個值在模型中訪問ApplicationControler變量
這是我的ApplicationController
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :base
def base
@empresa2 = current_usuario.empresa_id
end
end
這是我的模型Logdatabase
class Logdatabase < ActiveRecord::Base
self.abstract_class = true
if @empresa2 == "28"
establish_connection(:development)
else
establish_connection(:otrabase)
end
end
理想情況下,您不應該嘗試從模型中的控制器訪問變量。在MVC體系結構中,模型,視圖和控制器分別承擔責任,您應該堅持這一點。即使那樣,你想訪問,張貼你嘗試過的代碼,然後嘗試從社區獲得幫助 – Pramod
@Pramod好吧,我加了帖子。在這種情況下,我如何將ActionController變量傳遞給模型? – LuisC