[英]How to access session variables across multiple controllers in Rails?
I have implemented a facebook connect for my application. 我已经为我的应用程序实现了facebook connect。 So when a user logs in I wish to store his facebook id in the session and use it across multiple controllers and views. 因此,当用户登录时,我希望将其Facebook ID存储在会话中,并在多个控制器和视图中使用它。 I tried implementing this . 我试图实现这一点。 I also referred to several similar questions answered in stackoverflow . 我还提到了在stackoverflow中回答的几个类似问题。 However something seems wrong. 但是,似乎有些问题。 I set the facebook id in the session in the UserController . 我在UserController的会话中设置了Facebook ID。 I am not able to access it in the EventsController. 我无法在EventsController中访问它。
The flow is : When the user logins in for the first time . 流程为:用户首次登录时。 The fbinsertupdate is called. 调用fbinsertupdate。 And then the page redirects to events#index page. 然后页面重定向到events#index页面。
class User < ActiveRecord::Base
def self.current=(u)
@current_user = u
end
def self.current
@current_user
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# Finds the User with the ID stored in the session with the key
# :fb_user_id
def current_user
User.current ||= session[:fb_user_id] &&
User.find(session[:fb_user_id])
end
end
class UsersController < ApplicationController
def fbinsertupdate #Facebook connect. Insert new user or update existing user
@user = User.find_or_create_by_fbid(params[:fbid])
@user.update_attributes(params[:user])
session[:fb_user_id] = @user.fbid # storing the facebook id in the session
render :nothing => true
end
end
class EventsController < ApplicationController
def index
@events = Event.all
@current_user= User.current
logger.debug "The current user is "+@current_user.name # This fails . Says the class in nil
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @events }
end
end
end
Is there a before_filter
missing? 是否缺少before_filter
? You do not seem to call the current_user
method at all (that reads the session). 您似乎根本没有调用current_user
方法(读取会话)。 I see two ways to correct this: 我看到两种纠正方法:
Either use a before_filter
: at the top of your EventsController
write 可以使用before_filter
:在EventsController
的顶部编写
class EventsController < ApplicationController
before_filter :current_user
def index
@events = Event.all
@current_user= User.current
logger.debug "The current user is "+@current_user.name # This fails . Says the class in nil
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @events }
end
end
end
or, you change your index
-code as follows: 或者,您按以下方式更改index
代码:
def index
@events = Event.all
@current_user= current_user
logger.debug "The current user is "+@current_user.name # This fails . Says the class in nil
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @events }
end
end
Note that if you are using an API, using rest/soap, that there is no session and that you will have to return information (eg xml or json), that then could be handed along in the next call. 请注意,如果您使用的是API,并使用rest / soap,则没有会话,并且您将必须返回信息(例如xml或json),然后可以在下一次调用中处理该信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.