title: How can I declare a global variable in Ruby on Rails?
body: <p>How can I declare a global variable in Ruby on Rails?</p>
  
  <p>My sample code:</p>
  
  <p>in my <code>controller#application.rb</code>:</p>
  
  <pre><code>def user_clicked()
    @current_userid = params[:user_id]
  end
  </code></pre>
  
  <p>in my <code>layout#application.html.haml</code>
  I have sidebar with this link:</p>
  
  <pre><code>= link_to "John", user_clicked_path(:user_id =&gt; 1)
  = link_to "Doe", user_clicked_path(:user_id =&gt; 2)
  = link_to "View clicked user", view_user_path
  </code></pre>
  
  <p>in my <code>views#view_user.html.haml</code>:</p>
  
  <pre><code>%h2 @current_userid
  </code></pre>
  
  <p>I want to declare a global variable that can modify my controller and use it anywhere, like controller, views, and etc. The above is only a sample scenario. If I click the John or Doe link, it will send a <code>user_id</code> to the controller and when I click the "View clicked user" link, it will display the last clicked link. It is either <code>John=1</code> or <code>Doe=2</code>.</p>
  
  <p>Of course if I click the "View clicked user" link first, it will display <code>nil</code>.</p>