curious how params, flash etc work in rails ?

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 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:

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

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

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

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 (key)     @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