title: Rails assign session variable
body: <p>Hi I'm hacking around Rails the last 8 months so don't truly understand a number of things. I've seen and used helper methods that set the current_user etc. and am now trying to replicate something similar. </p>
  
  <p>I'm trying to set a global value in the application controller for the current company the user is in. I know I'm doing it wrong but I can't quite figure out how to solve it or even if I'm approaching it correctly. I want it so that when the user clicks on a company, the global variable is set to the id of the company they are in. Then in any other sub-model, if I want info on the company, I use the global variable to retrieve the company object with that id.</p>
  
  <p>The code that's causing the problem is in my navbar in <strong>application.html.erb</strong></p>
  
  <pre><code>&lt;li&gt;&lt;%= link_to "Company", company_path, :method =&gt; :get %&gt;&lt;/li&gt;
  </code></pre>
  
  <p>This works when I'm using the companies controller etc. But when I try use any other controller I'm getting the error</p>
  
  <blockquote>
    <p>ActionController::UrlGenerationError in Employees#index</p>
  </blockquote>
  
  <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"companies"} missing required keys: [:id] 
  </code></pre>
  
  <p>which as I understand it means it can't render the url because no company id parameter is being passed?</p>
  
  <p>I was trying to hack a helper method I used in another project (for getting the current_user) but have realised that it uses the session to extract the user.id to return a user object. Here's my attempt at the helper method in <strong>application_controller.rb</strong></p>
  
  <pre><code>def current_company
    @company = Company.find_by_id(params[:id])
  end
  </code></pre>
  
  <p>I'm not able to get the current company from the session so my method above is useless unless the company id is being passed as a parameter.</p>
  
  <p>I've thought about passing the company id on every sub-model method but </p>
  
  <ol>
  <li>I'm not 100% sure how to do this</li>
  <li>It doesn't sound very efficient</li>
  </ol>
  
  <p>So my question is am I approaching this correctly? What's the optimal way to do it? Can I create a helper method that stores a global company id variable that gets set once a user accesses a company and can then be retrieved by other models? </p>
  
  <p>I probably haven't explained it too well so let me know if you need clarification or more info. Thanks for looking. </p>
  
  <blockquote>
    <p><strong><em>Edit 1</em></strong></p>
  </blockquote>
  
  <p>Made the changes suggested by Ruby Racer and now I have:</p>
  
  <blockquote>
    <p><strong>application.html.erb</strong></p>
  </blockquote>
  
  <pre><code> &lt;%unless current_page?(root_path)||current_page?(companies_path)||current_company.nil? %&gt;
  
     &lt;li&gt;&lt;%= link_to "Company", company_path, :method =&gt; :get %&gt;&lt;/li&gt;
  </code></pre>
  
  <p>This is not displaying the link in the navbar, I presume because current_company is nil (the other two unless statements were fine before I added current_company.nil?</p>
  
  <p>I'm setting the current_company in </p>
  
  <blockquote>
    <p><strong>companies_controller.rb</strong></p>
  </blockquote>
  
  <pre><code>before_action :set_company, only: [:show, :edit, :update, :destroy, :company_home]
  
  def company_home
    current_company = @company
    respond_with(@company)
  end
  </code></pre>
  
  <blockquote>
    <p><strong>application_controller.rb</strong></p>
  </blockquote>
  
  <pre><code>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 @company
    puts params.inspect
  end
  </code></pre>
  
  <p>Am I doing something wrong? </p>
  
  <blockquote>
    <p><strong><em>Edit 2</em></strong></p>
  </blockquote>
  
  <p>I have no idea why this isn't working. After the above edits, it appears as though the <code>session[:company_id]</code> is not being assigned so the <code>current_company</code> helper method is returning nil. I've tried printing the session paramaters <code>puts session.inspect</code> and can't find any company_id information. Anyone any idea why it isn't assigning the value? </p>
  
  <blockquote>
    <p><strong>Edit 3</strong></p>
  </blockquote>
  
  <p>Can't for the life of me figure out what's going wrong. I've tried multiple things including moving the <code>current_company = @company</code> into the <code>set_company</code> method in <strong><code>companies_controller.rb</code></strong> which now looks like this:</p>
  
  <pre><code>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
      end      
    end
  </code></pre>
  
  <p>The <code>company_home</code> method is being given a company object (I can see this in the console output below) but the <code>current_company</code> assignment is just not happening. Here's the <strong>console output</strong> for reference</p>
  
  <pre><code>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"=&gt;"gfdhjfgjhoFFHGHGFHJGhjkdgkhjgdjhHGLKJGJHpDQs6yNjONwSyTrdgjhgdjgjf=", "id"=&gt;"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"=&gt;"get", "authenticity_token"=&gt;"gfdhjfgjhoFFHGHGFHJGhjkdgkhjgdjhHGLKJGJHpDQs6yNjONwSyTrdgjhgdjgjf=", "controller"=&gt;"companies", "action"=&gt;"company_home", "id"=&gt;"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"=&gt;"get", "authenticity_token"=&gt;"gfdhjfgjhoFFHGHGFHJGhjkdgkhjgdjhHGLKJGJHpDQs6yNjONwSyTrdgjhgdjgjf=", "controller"=&gt;"companies", "action"=&gt;"company_home", "id"=&gt;"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)
  </code></pre>
  
  <p>As per above, under the line <code>The current_company helper has been called</code>, there's a blank line where <code>puts @company</code> should be outputting something. This means the current_company method is returning nothing.</p>
  
  <p>Also, in the <code>company_home</code> method in the companies_controller, if I change <code>puts "#{@company.id} #{@company.name}"</code> to <code>puts "#{current_company.id} #{current_company.name}"</code> an error gets thrown.</p>
  
  <p>Has anyone any idea why the <code>def current_company=(company)</code> isn't assigning a session parameter? Thanks</p>
  
  <blockquote>
    <p><strong>Final Edit</strong></p>
  </blockquote>
  
  <p>I've no idea why, but it appears the problem related to this:</p>
  
  <pre><code>def current_company=(company)
    session[:current_company] = company.id
    puts "The current_company has been assigned"
    puts params.inspect
  end
  </code></pre>
  
  <p>It looks as though this never gets called. I don't understand why as I've never used something like this before.</p>
  
  <p>I'll put my fix in an answer.</p>