Forum: Ruby on Rails instance / class variable?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
rohitm2010 (Guest)
on 2005-11-20 00:28
(Received via mailing list)
hi all
  i want a variable to be accessed from more than one method of a
controller, (value same) & from .rhtml
  if i use a global var, application goes berserk, puttting one user's
data to another
  i know its some basic i missing ,
  but plz help, clear it
  thanks
  bye
petermichaux (Guest)
on 2005-11-20 00:37
(Received via mailing list)
I'm no expert but it looks like each action method of a controller is an
instance method. So I think you want an instance variable (@var) which
will
automatically be available in the .rthml file. If it doesn't work you
could
post here your trimmed down controller and view code.

Peter
ezra (Guest)
on 2005-11-20 02:59
(Received via mailing list)
On Nov 19, 2005, at 2:33 PM, Peter M. wrote:

> hi all
> i want a variable to be accessed from more than one method of a
> controller, (value same) & from .rhtml
> if i use a global var, application goes berserk, puttting one
> user's data to another
> i know its some basic i missing ,
> but plz help, clear it
> thanks
> bye
>
>


rohit-

	Put this in the top of your controller where you want to have a
shared var. I will assume the var is @foo

MyController < ApplicationController

    before_filter :setup_foo
    attr_reader :foo

   def setup_foo
       @foo = "Whatever you want @foo to contain"
   end

end


	Now @foo will be available to every action method in your controller
and also in your views for that controller. If you want @foo to be
available in all your controllers and view, put that code in your
application.rb ApplicationController.


HTH-

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
This topic is locked and can not be replied to.