Variable scope in rails controller

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.

On 3 December 2015 at 15:58, Sai Ch [email protected] wrote:

@var = “12gfjdl”
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs