Curious how params, flash etc work in rails?


#1

I am curious how stuff like params and flash work in the controller ?
Typically an instance variable is something like
@params, or maybe self.params. How does rails make params appear as if
it’s an instance variable ?


#2

I am curious how stuff like params and flash work in the controller ?
Typically an instance variable is something like
@params, or maybe self.params. How does rails make params appear as if
it’s an instance variable ?

I believe at one point params was an instance variable. And there was
a method “params” that passed through to that instance. I also
remember that this was changed awhile back and you should never ever
use @params again. That it is no longer a simple instance variable.

As for how they work specifically, dig through the source :slight_smile:


#3

On Wed, Mar 25, 2009 at 3:57 PM, removed_email_address@domain.invalid
removed_email_address@domain.invalid wrote:

I am curious how stuff like params and flash work in the controller ?
Typically an instance variable is something like
@params, or maybe self.params. How does rails make params appear as if
it’s an instance variable ?

irb

o = Object.new
=> #Object:0xb7a3e000

o.instance_variable_set( :@foo, ‘bar’ )
=> “bar”

o.inspect
=> “#<Object:0xb7a3e000 @foo=“bar”>”

http://www.ruby-doc.org/core/classes/Object.html#M000381


Greg D.
http://destiney.com/


#4

ok,

offhand I’m not sure how to create a method foo that is used like an
array so I can say foo[:id], but I’ll have to think about that or look
through my books …

On Mar 25, 5:46 pm, Frederick C. removed_email_address@domain.invalid


#5

On Mar 25, 8:57 pm, “removed_email_address@domain.invalid” removed_email_address@domain.invalid wrote:

I am curious how stuff like params and flash work in the controller ?
Typically an instance variable is something like
@params, or maybe self.params

@params is an instance variable, but self.params is a method called
params

How does rails make params appear as if
it’s an instance variable ?

it doesn’t. It just defines a method called params (and one called
flash etc…) on controllers. There use to be instance variables
called @params, @flash that you could use interchangeably but use of
those was deprecated and removed a while back.

Fred


#6

On Mar 25, 10:07 pm, Larz removed_email_address@domain.invalid wrote:

ok,

offhand I’m not sure how to create a method foo that is used like an
array so I can say foo[:id], but I’ll have to think about that or look
through my books …

There’s not much to it:

class Foo
def initialize
@hash = {}
end

def
@hash[key]
end

def []=(key,value)
@hash[key]=value
end
end

That’s not what’s happening with params though. That’s just a method
that returns a hash.

Fred