-1

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

Oatmeal
  • 759
  • 2
  • 9
  • 25

2 Answers2

1

You can move the method in ApplicationController and make it as helper method. Set session variable whenever you want to switch the division. You should store your division_id in session. Your current_division method should looks like:

  class ApplicationController < ActionController::Base
    ...
    ...

    private

    def current_division
      if session[:division_id]
        @current_division = Division.find_by_id(session[:division_id])
        session.delete(:division_id)
      else
        @current_division ||= current_user.application[0] # not sure what are you trying to do here
      end
    end

    helper_method :current_division
  end

You just need to call current_division and it will check if session[:division_id] exists and will update the division as needed. You just need to set session var when you want to switch and call current_division anytime anywhere.

RAJ
  • 9,697
  • 1
  • 33
  • 63
  • Can set the session[:division_id] = something in my Division Controller def set_current_app session[:division_id = params[:id] end – Oatmeal May 22 '15 at 07:07
  • Yes, you will set `session[:division_id]`, next time when you will call `current_division`, it will return you new switched division – RAJ May 22 '15 at 07:08
  • In the above solutuon, why do I need to session.delete(:division_id) – Oatmeal May 22 '15 at 09:17
  • if add the line session.delete(:division_id) , when I refresh the page, @current_division is all set back to default again... – Oatmeal May 22 '15 at 09:24
1

You can have a dropdown on navbar where you can load all the divisions. select the current division on dropdown. Pass the current division parameter on url params. check the params in application controller and set the divison.

class ApplicationController < ActionController::Base
  before_filter :set_current_division

  def set_current_division
     @current_division = Division.find_by_name(params[:division_name])
  end

end
Kamesh
  • 1,435
  • 1
  • 14
  • 27
  • In the above solution, @current_division is a global variable ? What do we call this kind of variables ? – Oatmeal May 25 '15 at 02:21
  • @Oatmeal current_division is a instance variable of ApplicationController. Actually all your controllers get inherited from ApplicationController have access to this variable. – Kamesh May 25 '15 at 05:39