I have a feedback form in my Rails application. The feedback form requires initializing of the @support variable, and it should be visible on every page. The initialization is very short:
@support = Support.new(:id => 1)
However it would be nice to have this variable initialized once and access it from everywhere. How is that possible to do?
you can use a helper method (in the application controller) to initialize the support variable . Something like this :
class ApplicationController < ..
...
helper_method :my_var
def my_var
@support = Support.new(:id => 1)
end
...
end
A global variable starts with the dollard sign '$' like :
$support = Support.new(:id => 1)
However, global variables is bad :-) You should read this post by "Simone Carletti".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With