Forum: Ruby on Rails curious how params, flash etc work in rails ?

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.
71477e5162d702dae2a072d66a855fec?d=identicon&s=25 wbsurfver@yahoo.com (Guest)
on 2009-03-25 21:58
(Received via mailing list)
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 ?
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-03-25 22:31
(Received via mailing list)
> 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 :)
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-03-25 22:36
(Received via mailing list)
On Wed, Mar 25, 2009 at 3:57 PM, wbsurfver@yahoo.com
<wbsurfver@gmail.com> 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 Donald
http://destiney.com/
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-25 22:46
(Received via mailing list)
On Mar 25, 8:57 pm, "wbsurf...@yahoo.com" <wbsurf...@gmail.com> 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
71477e5162d702dae2a072d66a855fec?d=identicon&s=25 Larz (Guest)
on 2009-03-25 23:08
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-25 23:24
(Received via mailing list)
On Mar 25, 10:07 pm, Larz <wbsurf...@gmail.com> 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 [](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
This topic is locked and can not be replied to.