Answer the question
In order to leave comments, you need to log in
How to use shared variables in a rails controller?
Hello.
Can you please tell me how to create variables that can be used (read and changed) from any Ruby on Rails controller method? Something like this is needed:
class ExController < ApplicationController
def common_variable
@com_var = 1
end
def first_action
puts @com_var
end
def second_action
@com_var = 2
end
end
first_action
>> 1
second_action
first_action
>> 2
first_action
>> nil
second_action
first_action
>> nil
Answer the question
In order to leave comments, you need to log in
To store data common to all controller methods, you should use the session mechanism:
guides.rubyonrails.org/action_controller_overview....
@var - class instance variable
@ @var - class variable
$var - global variable
Did you answer your own question?) Specify the question, please)
If you need to save the value of a variable between requests, then cookies or session.
Describe the problem more clearly. I'm sure there is a more elegant solution.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question