Want to return a global variable instead of a local variable
I am verifying the existence of "test_results" in the application controller. It is returned as a local variable. I would like to call it once and have it available for the whole session. How do i do that?
"test_results" in application controller:
def test_results
(0 .. 4).each do |x| # looks for answers to the first 4 questions
if @answers[x].nil? || @answers[x] == 0.0
return false
break
end
end
return tr开发者_Go百科ue
end
other controllers:
before_filter :test_results
if test_results
...do stuff
else
...display "take the test"
end
error message from view:
undefined local variable or method `test_results'
You can try using @
if @instrument_results
Edit
The result of before filter isn't stored. I don't know what you want to do but you can set a controller level variable inside the test_results function and then referer it as @controller.variable_name. But if your intention is to make a conditional view based on test_results return value, I suggest you to make a redirect to another action in the test_results method and put the success content in the current controller. You can use :only and :except modifier to check which action of the controller will call the :before_filter. Hope this helps.
精彩评论