Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Rails Assign a session value

Options
  • 19-12-2014 11:30am
    #1
    Closed Accounts Posts: 7,967 ✭✭✭


    I've spent hours at this but can't figure out why it's not working. I'm trying to assign the id of the company the user is currently in to a session variable that can be accessed by other models. But whenever I call the current_company method it keeps returning nil.

    I'm using Devise but I don't think that's the problem.

    The set_company method is working on the companies_controller because I can see the company values in my debug in the company_home method when that page is called.

    This is the first time I've ever used assignments for helpers (current_company=(company)) and am not sure if that's correct or not. Someone on stackoverflow gave me the code. Is this assignment the problem? It seems to be as otherwise the code should work and current_company.nil? would not return true.

    I've tried to inspect the session params using puts session.inspect but the output is huge and I can't anything one called :current company. Is there a simple way to just print a particular session paramater?

    Any ideas? I'm at a loss at this stage.

    [B]application_controller.rb[/B]
    
     helper_method :address_string, :current_company
    
     def current_company=(company)
        session[:current_company] = company.id
        puts "The current_company has been assigned"
        puts params.inspect
      end
    
      def current_company
      	@company = Company.find_by_id(session[:current_company])
      	puts "The current_company helper has been called"
      	puts params.inspect
      end
    
    
    [B]companies_controller.rb[/B]
    
      def company_home
        puts "Test the current company"
        puts "#{@company.id} #{@company.name}"
        puts params.inspect
      end
    
      private
        def set_company
          @company = Company.find_by_id(params[:id])
          if @company.nil?||current_user.organisation_id != @company.organisation.id
              flash[:alert] = "Stop poking around you nosey parker"
              redirect_to root_path
          else
    	current_company = @company # I've also tried putting this in the company_home method but it didn't work there either
          end      
        end
    
    


    Here's the console output for reference
    Started GET "/company_home/1" for 80.55.210.105 at 2014-12-19 10:26:49 +0000                                                                                                      
    Processing by CompaniesController#company_home as HTML                                                                                                                            
      Parameters: {"authenticity_token"=>"gfdhjfgjhoFFHGHGFHJGhjkdgkhjgdjhHGLKJGJHpDQs6yNjONwSyTrdgjhgdjgjf=", "id"=>"1"}                                                                                   
      User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 6  ORDER BY "users"."id" ASC LIMIT 1                                                                    
      Company Load (0.3ms)  SELECT  "companies".* FROM "companies"  WHERE "companies"."id" = 1 LIMIT 1                                                                                
      Organisation Load (0.3ms)  SELECT  "organisations".* FROM "organisations"  WHERE "organisations"."id" = $1 LIMIT 1  [["id", 6]]                                                 
    Test the current company                                                                                                                                                          
    1 Cine                                                                                                                                                                            
    {"_method"=>"get", "authenticity_token"=>"gfdhjfgjhoFFHGHGFHJGhjkdgkhjgdjhHGLKJGJHpDQs6yNjONwSyTrdgjhgdjgjf=", "controller"=>"companies", "action"=>"company_home", "id"=>"1"}                          
      Rendered companies/company_home.html.erb within layouts/application (0.1ms)                                                                                                     
      Company Load (0.6ms)  SELECT  "companies".* FROM "companies"  WHERE "companies"."id" IS NULL LIMIT 1                                                                            
    The current_company helper has been called                                                                                                                                        
                                                                                                                                                                                      
    {"_method"=>"get", "authenticity_token"=>"gfdhjfgjhoFFHGHGFHJGhjkdgkhjgdjhHGLKJGJHpDQs6yNjONwSyTrdgjhgdjgjf=", "controller"=>"companies", "action"=>"company_home", "id"=>"1"}                          
      CACHE (0.0ms)  SELECT  "organisations".* FROM "organisations"  WHERE "organisations"."id" = $1 LIMIT 1  [["id", 6]]                                                             
    Completed 200 OK in 280ms (Views: 274.0ms | ActiveRecord: 1.7ms)   
    


Comments

  • Closed Accounts Posts: 7,967 ✭✭✭Synode


    No rails experts round here? This is doing my head in.


  • Registered Users Posts: 363 ✭✭Rockn


    current_company = @company
    
    is assigning @company to a local variable.

    You need:
    self.current_company = @company
    


  • Closed Accounts Posts: 7,967 ✭✭✭Synode


    Thanks for the reply. I ended up changing it and assigning the session variable in the set_company method.

    If I use assignment similar to the above again I'll use self.current_company


Advertisement