I''m trying to fetch a session (and other fb user variables from the session) at the start of my controller, so that all the actions in that controller can make use of it. Here''s the code: class FacebookController < ApplicationController ensure_application_is_installed_by_facebook_user fb_session = session[:facebook_session] # ... action functions here ... end I''m getting this error: TypeError (Symbol as array index): .//app/controllers/facebook_controller.rb:3:in `[]'' .//app/controllers/facebook_controller.rb:3 If I put the session[:facebook_session] reference inside any of the actions, it works fine. But that doesn''t allow me to factor it out for use by all actions. If I put it inside a helper function or class method, it gives the same error as above. So how do I access the current session outside of an action function, so that I can factor out the session setup code common to all the actions? Thanks, Shawn
Hey Shawn, If I am reading your code right you are tring to access ''session'' in the class level context, session is an instance method. What you want here is another method and then you can use it as a before filter. class FacebookController < ApplicationController ensure_application_is_installed_by_facebook_user before_filter :setup_stuff def setup_stuff @fb_user = @facebook_session.user @current_friends = @fb_user.friends! ....... end ..... BTW: After further code reading I noticed that ensure_application_is_installed_by_facebook_user allready sets up the session in an instance variable "@facebook_session", my previous advice didn''t go far enough. Dave On Jan 23, 2008 3:18 AM, Shawn Van Ittersum <svicalifornia at gmail.com> wrote:> I''m trying to fetch a session (and other fb user variables from the session) at the start of my controller, so that all the actions in that controller can make use of it. Here''s the code: > > class FacebookController < ApplicationController > ensure_application_is_installed_by_facebook_user > fb_session = session[:facebook_session] > > # ... action functions here ... > end > > I''m getting this error: > > TypeError (Symbol as array index): > .//app/controllers/facebook_controller.rb:3:in `[]'' > .//app/controllers/facebook_controller.rb:3 > > If I put the session[:facebook_session] reference inside any of the actions, it works fine. But that doesn''t allow me to factor it out for use by all actions. If I put it inside a helper function or class method, it gives the same error as above. So how do I access the current session outside of an action function, so that I can factor out the session setup code common to all the actions? > > Thanks, > Shawn > _______________________________________________ > Facebooker-talk mailing list > Facebooker-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/facebooker-talk >
Thanks, Dave! Shawn On Wed, 23 Jan 2008 07:28:53 -0700, David Clements wrote:> Hey Shawn, > > If I am reading your code right you are tring to access ''session'' in > the class level context, session is an instance method. What you want > here is another method and then you can use it as a before filter. > > > class FacebookController < ApplicationController > ensure_application_is_installed_by_facebook_user > before_filter :setup_stuff > > def setup_stuff > @fb_user = @facebook_session.user > @current_friends = @fb_user.friends! > ....... > end > > ..... > > > BTW: After further code reading I noticed that > ensure_application_is_installed_by_facebook_user allready sets up the > session in an instance variable "@facebook_session", my previous > advice didn''t go far enough. > > Dave > > > > > > > On Jan 23, 2008 3:18 AM, Shawn Van Ittersum <svicalifornia at gmail.com> wrote: >> I''m trying to fetch a session (and other fb user variables from the >> session) at the start of my controller, so that all the actions in >> that controller can make use of it. Here''s the code: >> >> class FacebookController < ApplicationController >> ensure_application_is_installed_by_facebook_user >> fb_session = session[:facebook_session] >> >> # ... action functions here ... >> end >> >> I''m getting this error: >> >> TypeError (Symbol as array index): >> .//app/controllers/facebook_controller.rb:3:in `[]'' >> .//app/controllers/facebook_controller.rb:3 >> >> If I put the session[:facebook_session] reference inside any of the >> actions, it works fine. But that doesn''t allow me to factor it out >> for use by all actions. If I put it inside a helper function or >> class method, it gives the same error as above. So how do I access >> the current session outside of an action function, so that I can >> factor out the session setup code common to all the actions? >> >> Thanks, >> Shawn >> _______________________________________________ >> Facebooker-talk mailing list >> Facebooker-talk at rubyforge.org >> http://rubyforge.org/mailman/listinfo/facebooker-talk >>