[英]Rails- Ruby Instance Variable
So I am using omniauth-facebook to create a log in using Facebook. 因此,我正在使用omniauth-facebook来创建使用Facebook的登录。
Here is my sessions_controller.rb
这是我的
sessions_controller.rb
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to "/sessions/menu"
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
def new
end
def menu
puts user.name
end
end
Unfortunately I don't know how to access the user variable in the menu action. 不幸的是,我不知道如何在菜单操作中访问用户变量。 How would you guys recommend I do this?
你们会如何建议我这样做?
Update 更新资料
class SessionsController < ApplicationController
def create
@user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = @user.id
redirect_to "/sessions/menu"
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
def new
end
def menu
puts @user
end
end
Even when I update it like so, it doesn't work 即使我像这样更新它,也不起作用
Unfortunately I don't know how to access the user variable in the menu action.
不幸的是,我不知道如何在菜单操作中访问用户变量。 How would you guys recommend I do this?
你们会如何建议我这样做?
Every time a request is made in your app for actions in SessionsController
, a new instance of the SessionsController
is created. 一个请求在您的应用程式的行为使每次
SessionsController
的的新实例SessionsController
创建。 So, instance variable set during create
action would not be available when request for menu
action is called as now you have a new instance of SessionsController
which is why @user
set in create
action would not be available in menu
. 因此,当调用
menu
操作请求时,在create
操作期间设置的实例变量将不可用,因为现在您有了SessionsController
的新实例,这就是为什么在create
action中设置的@user
在menu
不可用的原因。 Also, if you use user
( local variable ) in this case, then its always local to the method/action in which its defined. 另外,如果在这种情况下使用
user
( 局部变量 ),则它始终位于定义其的方法/操作的局部。 So even that would not be available in menu
. 因此,即使在
menu
也不可用。
By using facebook-omniauth
gem you would receive Auth Hash in env["omniauth.auth"]
which in turn you would use to create(new user) or initialize(in case of existing user) a user hopefully in from_omniauth
method. 通过使用
facebook-omniauth
gem,您将在env["omniauth.auth"]
收到facebook-omniauth
验证哈希,而您又可以希望使用from_omniauth
方法来创建(新用户)或初始化(如果存在现有用户)用户。 NOTE: env
is request specific so env["omniauth.auth"]
value will be present in create
action but not in menu
. 注意:
env
是特定于请求的,因此env["omniauth.auth"]
值将出现在create
动作中,但不会出现在menu
。
To resolve this, ie, to access the created or initialized facebook user
in menu
action, you should make use of the user_id
that you stored in session
as below: 要解决此问题,即在
menu
操作中访问已创建或初始化的facebook user
,应使用存储在session
中的user_id
,如下所示:
def menu
if session[:user_id]
@user = User.find(session[:user_id])
end
end
Also, if you would like to access the user in other actions as well then I would recommend to reuse the code by using before_action
callback: 另外,如果您也想通过其他操作访问用户,那么我建议通过使用
before_action
回调来重用代码:
class SessionsController < ApplicationController
## Pass specific actions to "only" option
before_action :set_user, only: [:menu, :action1, :action2]
#...
def menu
puts @user.name
end
private
def set_user
if session[:user_id]
@user = User.find(session[:user_id])
end
end
end
where you can add specific actions via :only
option 您可以在其中通过
:only
选项添加特定操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.