19

How can I declare a global variable in Ruby on Rails?

My sample code:

in my controller#application.rb:

def user_clicked()
  @current_userid = params[:user_id]
end

in my layout#application.html.haml I have sidebar with this link:

= link_to "John", user_clicked_path(:user_id => 1)
= link_to "Doe", user_clicked_path(:user_id => 2)
= link_to "View clicked user", view_user_path

in my views#view_user.html.haml:

%h2 @current_userid

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 user_id to the controller and when I click the "View clicked user" link, it will display the last clicked link. It is either John=1 or Doe=2.

Of course if I click the "View clicked user" link first, it will display nil.

Karol Selak
  • 4,248
  • 6
  • 35
  • 65
do_Ob
  • 709
  • 1
  • 6
  • 24
  • 1
    Sorry, I didn't understand your question. – Surya May 19 '15 at 09:36
  • Check [This](http://stackoverflow.com/questions/12112765/how-to-reference-global-variables-and-class-variables) – Abhi May 19 '15 at 09:44
  • Hi... Sorry for my bad grammar ^_^ I edited my question ^_^ and sample scenario. What I am trying to say is I want a global variable that I can access anywhere. like the example above, if I clicked John or Doe link, it will send a user_id to the controller and when I click the "View clicked user" link, it will display the last clicked link, it is either John=1 or Doe=2. Ofcourse if I clicked "View clicked user" link first, it will display nil. ^_^ Please help ^_^ – do_Ob May 19 '15 at 13:31

1 Answers1

26

In Ruby global variables are declared by prefixing the identifier with $

$foo = 'bar'

Which you rarely see used for a number of reasons. And it's not really what you are looking for.

In Ruby instance variables are declared with @:

class DemoController
  def index
    @some_variable = "dlroW olleH"
    @some_variable = backwards
  end
  private 
  def backwards
     @some_variable.reverse
  end
end

Rails automatically passes the controller's instance variables to the view context.

# /app/views/demos/index.html.haml
%h1= @some_variable 

Guess what it outputs and I'll give you a cookie.

In your example @global_variable is nil since controller#sample_1 is not called - the request would go through controller#sample_2.

def sample_2
  @global_variable = "Hello World"
end
max
  • 96,212
  • 14
  • 104
  • 165
  • Hi... Thank you for the reply ^_^ I edited my question above ^_^ and change the sample scenario. What I am trying to say is I want a global variable that I can access anywhere. If I clicked John or Doe link, it will send a user_id to the controller and when I click the "View clicked user" link, it will display the last clicked link, it is either John=1 or Doe=2. Ofcourse if I clicked "View clicked user" link first, it will display nil. Sorry for my bad grammar ^_^ – do_Ob May 19 '15 at 13:29
  • 1
    I think what you are looking for may be sessions http://guides.rubyonrails.org/action_controller_overview.html#session – max May 19 '15 at 13:39
  • 1
    Thanks for this sessions ^_^... I tried and I am using it now everywhere... session[:global_variable] – do_Ob May 20 '15 at 13:00