Facebook注册后重定向到页面 [英] Redirect to Page after Facebook Sign Up
问题描述
我想重定向到 / getstarted / welcome
在用户首次注册 之后
我的omniauth回调是:
def facebook
#您需要在模型中实现以下方法(例如app / models / user.rb)
@user || =
User.find_for_facebook_oauth(request.env [omniauth.auth],current_user)
如果@ user.persisted?
#如果@user没有被激活,这将抛出
sign_in_and_redirect @user,event::authentication
if is_navigational_format?
set_flash_message(:notice,:success,kind:Facebook)
end
else
session [devise.facebook_data] = request.env [omniauth.auth ]
redirect_to new_user_registration_url
end
end
对于devise我使用
def after_sign_up_path_for(来源)
'/ getstarted / welcome'
end
我的用户型号:
Facebook设置
def self.find_for_facebook_oauth(auth,signed_in_resource = nil)
user = User.where(provider:auth.provider,uid:auth.uid ).first
如果user.present?
user
else
user = User.create(first_name:auth.extra.raw_info.first_name,
last_name:auth.extra.raw_info.last_name,
facebook_link: auth.extra.raw_info.link,
user_name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth。 info.email,
密码:Devise.friendly_token [0,20])
end
end
有人可以帮我设置吗?
我通过添加到我的用户模型
attr_accessor`just_signed_up`
并将其设置为 User.find_for_facebook_oauth
在我创建新用户( first_or_create
块)的块的这一部分。
编辑:更多的解释
所以在Ruby(不是Rails)中有一个类方法/宏叫做 attr_accessor
(实际上theres也是 attr_reader
和 attr_writer
, attr_accessor
是一个简写调用另外两个)
如果您在用户模型中写入
class User
attr_accessor:some_attribute
那么你可以执行
u = User.first
u.some_attribute ='asdf'
u.some_attribute#=> 'asdf'
但是这个属性不会保存到DB,所以它可以被用作在Rails模型中临时存储一些值。
另一件要知道的是,Ruby中只有两个值是false,那些是 false
和 nil
。
使用这两个技巧,您可以创建一个新的用户并临时在对象上设置此标志
u = User.create
u.just_signed_up = true
u.just_signed_up#=> true
u.reload! #从DB
u.just_signed_up#=> nil
中获取记录,因为nil为false,所以除了刚才创建的用户之外,此检查将失败!
I'm trying to Redirect a user after successful Facebook SIGN UP (not sign in).
I want to redirect to /getstarted/welcome
after a user Registers for the first time.
My omniauth callback is :
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user ||=
User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
# This will throw if @user is not activated
sign_in_and_redirect @user, event: :authentication
if is_navigational_format?
set_flash_message(:notice, :success, kind: "Facebook")
end
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
For devise i use
def after_sign_up_path_for(source)
'/getstarted/welcome'
end
My User Model:
Facebook Settings
def self.find_for_facebook_oauth(auth, signed_in_resource = nil)
user = User.where(provider: auth.provider, uid: auth.uid).first
if user.present?
user
else
user = User.create(first_name:auth.extra.raw_info.first_name,
last_name:auth.extra.raw_info.last_name,
facebook_link:auth.extra.raw_info.link,
user_name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20])
end
end
Can someone help me set this up ?
I solved it by adding to my User model
attr_accessor `just_signed_up`
and setting it in User.find_for_facebook_oauth
in this part of the block where I create a new user (first_or_create
block).
EDIT: more explanation
So in Ruby (not Rails) there is a class method/macro called attr_accessor
(actually theres also attr_reader
and attr_writer
, attr_accessor
is a shorthand for calling the other two)
If you, in your User model write
class User
attr_accessor :some_attribute
then you're able to perform
u = User.first
u.some_attribute = 'asdf'
u.some_attribute # => 'asdf'
but this attribute is not going to be saved to DB, so it may be used as a temporary storage of some value in Rails model.
Another thing to know is that there are only two "values" that are false in Ruby, those are false
and nil
.
Using those two tricks you may create a new user and temporarily set this flag on the object
u = User.create u.just_signed_up = true u.just_signed_up # => true u.reload! # fetches record from DB u.just_signed_up # => nil
and since nil is false, this check will fail for every user except for ones you just created!
这篇关于Facebook注册后重定向到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!