Forum: Ruby on Rails Using same variable in controller

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.
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-26 03:53
Hi guys!  More question :P~

OK, I'm following a tutorial for rail, and I got this new.html.erb in
views, and in order for a form to be showed up correctly without error,
I had to add this small snippet of code in the controller under method
"def new".

 class AdsController < ApplicationController
  2   def show
  3     @ad = Ad.find(params[:id])
  4   end
  5
  6   def index
  7     @ads = Ad.find(:all)
  8   end
  9
 10   def new
 11     @ad = Ad.new
 12   end
 13 end

My question is, I thought this would be creating an error because I have
def show using variable @ad, and then def new also using variable @ad,
but instead of any glitch, I got the rail app works out perfectly.

I'm new to ruby and the programming world and especially rail.  I just
wonder is this a good practice?  It's like overwrite what @ad in "def
show" when user wants to get action "def new."  Or maybe I got confused?
Rails has all variable in each method as local?  Or each time each
action/method gets executed, it will be newly object in memory anyway,
and so it won't be affected by the code/abstract above?  Please help me
understand this, thanks...
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-26 04:05
(Received via mailing list)
Power One wrote:

> My question is, I thought this would be creating an error because I have
> def show using variable @ad, and then def new also using variable @ad,
> but instead of any glitch, I got the rail app works out perfectly.

Welcome to "event driven" land.

If you wrote a new class, and did not inherit ApplicationController, you
would
create class instances with .new.

    x = MyClass.new

Instance variables - beginning with @ - would live as long as x did.

A Controller class is different. Rails creates an instance for you, each
time
the routes.rb map tells Rails to. Then this object lives long enough to
service
one and only one action.

Then it dies. All its instance variables go away, too.

So you can declare different sets of instance variables - or even the
same set -
in each action, and Rails generally keeps them out of each others' hair.

Now please read a book on Ruby, straight thru. Typing in tutorials is
fun, but
it burdens this newsgroup with content that you should be learning for
yourself!

--
   Phlip
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-03-26 04:41
(Received via mailing list)
Sent from my iPhone

On Mar 25, 2009, at 7:53 PM, Power One <rails-mailing-list@andreas-
s.net> wrote:

>  2   def show
> 13 end
> Rails has all variable in each method as local?  Or each time each
> action/method gets executed, it will be newly object in memory anyway,
> and so it won't be affected by the code/abstract above?  Please help
> me
> understand this, thanks...
> --
> Posted via http://www.ruby-forum.com

Andreas, the instance variable is set when the action is invoked.  For
example,

http://www.example.com/controller/action

Thus, I would recommend reading one of the relevant guides at

guides.rubyonrails.org

Good luck,

-Conrad
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-03-26 18:13
Phlip wrote:
> A Controller class is different. Rails creates an instance for you, each
> time
> the routes.rb map tells Rails to. Then this object lives long enough to
> service
> one and only one action.
>
> Then it dies. All its instance variables go away, too.

Even if this were not the case reusing that instance variable would not
cause any errors or problems.

When the action is called the variable gets assigned a value. If the
instance variable already had a value then the old value would be
dereferenced by the assignment. Then the variable would be set to
reference the new value. The memory that was previously used by the old
value would subsequently be freed by the Ruby automatic garbage
collector.
This topic is locked and can not be replied to.