In my application, I have Company, Division, Users.... Company - have many Divisions, Users - belongs to one or more Divisions.
When user login, the current_division is set to User.division[0] in the Application_controller. I want to implement a feature where user can switch between different divisions. Any ideas about how to implement this functionality.
How to change current_division in the Application_controller? This current_division is used by other controllers and models.
Class Division < ActiveRecord:Base
def self.current_div options={}
if options[:division_id].nil?
current_user.division[0]
else
Division.find_by_id(options[:id])
end
end
I call Division.current_div method when user choose a different division from dropdown
You can move the method in
ApplicationController
and make it as helper method. Setsession
variable whenever you want to switch the division. You should store yourdivision_id
insession
. Yourcurrent_division
method should looks like:You just need to call
current_division
and it will check ifsession[:division_id]
exists and will update the division as needed. You just need to set session var when you want to switch and callcurrent_division
anytime anywhere.