On 3 December 2015 at 15:58, Sai Ch <li...@ruby-forum.com> wrote: > Hi, > > I would like to know variable scope for variable accessibility in all > rails controller methods. > > for example, I had done like below. > > class app_controller > > def set_vaule > @var = "12gfjdl" > end > > def actions1 > need display @var here > end > > def action2 > here also need @var value > end > > end > > note: I had tried with session and declare @var as class variable(@@var) > it access only first time, later if I made any request again the action > is set to nil i.e > uninitialised class variable.
An instance variable (@...) is available in any method. Note though that since the controller is reconstructed for each request it will not survive between requests (so in the above you might want a before_filter calling set_value). To have a persistent object that survives over multiple requests (per user) then use the session. If something is always a fixed value then use a constant. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtbvov%3DD%3D%3DcgKCB4i4V_WFvcET4jg_jw78bhCqa87Ym5Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.