Using same variable in controller


#1

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…


#2

Power O. 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


#3

Sent from my iPhone

On Mar 25, 2009, at 7:53 PM, Power O. <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


#4

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.