How to access global variable in a view in Ruby on Rails?

Posted by ben on Stack Overflow See other posts from Stack Overflow or by ben
Published on 2010-05-23T10:16:48Z Indexed on 2010/05/23 10:20 UTC
Read the original article Hit count: 282

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!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about global-variables