[Rails] Re: A Good Tutorial on Params and Variables

Adam Denenberg
2006-04-30T13:12:06Z

just be careful assigning a model object to a session. Especially if you
have associations and things the sessions can get enormous (has_many, etc).
Also changes to the model wont be reflected in the session so just be
careful of that as well.

I needed all the attributes from the User model as well in my session so i
just did something like this to prevent all the associations from making my
session huge

session[:user] = Hash.new
session[:user] = @user.attributes

Now you can access things like session[:user]["user_id"] and
session[:user]["username"]

adam

On 4/30/06, Ted Battreall <ted@battreall.com>
wrote:
>
> One more question. You said "Note, if you are embedding a
''class''
> into sessions, you also need to specify a ''model'' so
other controllers
> know how to process the chunk of data specified in the session".
>
> I think this may be the root of my session problems.  I wanted to store
> the user information after login, so I can use it throughout my
> application.
>
> I used the syntax you described:
> session[:user] ||= @user
>
> This works fine within the Controller it was assigned it, but it has a
> Nil value when I try to reference it in other Controllers. You mentioned
> that I need to specify a model. What is the syntax for doing this?
>
> Specifically here is the code:
>
> class UserController < ApplicationController
>   model   :user
>   layout  ''scaffold''
>
>   def login
>     return if generate_blank
>     @user = User.new(@params[''user''])
>     if session[''user''] =
User.authenticate(@params[''user''][''login''],
> @params[''user''][''password''])
>       flash[''notice''] = l(:user_login_succeeded)
>       session[:user] ||= @user
> ...
>
> class UserNotify < ActionMailer::Base
>   def invite(person)
>     @recipients = person.email
>     @from = @session[:user].email
> ...
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...