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
.