views:

504

answers:

3

I have a User model.

I have a Session controller, in which I have a global user variable that is assigned as follows:

$user = User.authenticate(params[:session][:email], params[:session][:password])

(I've made user global just to try to solve this problem, so if there's a better way please let me know!)

I need to use the email of the logged in user as a parameter to send to Flex part of my website. At the moment I'm creating the link as follows:

<%= link_to "secondpage", secondpage_path(:email => @session.$user.email) 

But I'm getting the following error:

compile error /Users/benhartney/rails_projects/talk/app/views/layouts/_header.html.erb:12: syntax error, unexpected tGVAR ..._path(:email => @session.$user.email) ).to_s); @output_buffe...

There's also a little arrow pointing at $user

If I remove the $ from $user, I get this error:

undefined method `user' for nil:NilClass

If I remove the

(:email => @session.user.email)

part, everything works fine, so I think all of the code except for this is ok.

Can anyone tell me what I'm doing wrong?

Thanks for reading!

+1  A: 

You can't have a global variable local to something, as far as I understand (this time, you can't have a global variable that is local to session)

Try using :email => $user.email if you're adamant that you need a global variable for something.

alamar
A: 

You can also try

Thread.current[:email]
scottschulthess
+1  A: 

I think what you are looking for is a session variable.

Rails is primarily a framework for creating web applications, and http is stateless, i.e. each request knows nothing about what went on before. To get over this limitiation, web developers use cookies or session variables to simulate maintenance of state, i.e. data, across requests.

Check out http://guides.rubyonrails.org/action_controller_overview.html#session

stephenr
Thanks so much, this worked perfectly.
ben