2013-03-20 115 views
1

我有Rails应用程序与宝石设计+宝石omniauthn(多个提供商)。我也跟着这个教程:带有omniauth注册中必填字段的表单。 (Ruby on Rails)

用户模型:

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable 
    attr_accessible :name, :email, :country, :password, :password_confirmation, :remember_me 
    has_many :authentications, :dependent => :delete_all 

    def apply_omniauth(auth) 
    self.email = auth['extra']['raw_info']['email'] 
    authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token']) 
    end 

认证模型:http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/https://github.com/klebershimabuku/fb_auth_demo应用的示例)

attr_accessible :provider, :token, :uid, :user_id 
belongs_to :user 

认证控制器:

def create 
    auth = request.env["omniauth.auth"] 

    # Try to find authentication first 
    authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid']) 

    if authentication 
     # Authentication found, sign the user in. 
     flash[:notice] = "Signed in successfully." 
     sign_in_and_redirect(:user, authentication.user) 
    else 
     # Authentication not found, thus a new user. 
     user = User.new 
     user.apply_omniauth(auth) 
     if user.save(:validate => false) 
     flash[:notice] = "Account created and signed in successfully." 
     sign_in_and_redirect(:user, user) 
     else 
     flash[:error] = "Error while creating a user account. Please try again." 
     redirect_to root_url 
     end 
    end 
    end 

当我注册了设计我可以填写一些额外的字段,如姓名和国家。但是当我用omniauth注册时,我无法填充这些字段,因为它根本没有形成,所以它们在注册后是NULL。所以我的问题:我怎样才能添加一个表单所需的额外字段omniauth注册?

+0

你有没有发现,如何做到这一点? – Cyzanfar 2016-10-20 04:12:12

回答

0

Omniauth用于使用其他登录信息登录,您的网站 - 像Facebook,微博等

因此,当用户使用omniauth登录到你的网站,你可以收集来自网站用户信息正在使用登录到您的网站。

例如:如果用户正在使用来自Facebook的登录,则可以从用户用于登录的站点收集用户从该用户处获得的该城市和其他信息。所以更多的信息,你可以从(伪)获得:

auth['extra']['raw_info']['state'] 

编辑: 哈氏此说,你可以按照这个railcast 信息如何添加其他字段来注册。

此外,另一种方式来遵循创建设备/ omniauth是遵循this page(随便说)

+0

这是omniauth mannnn的一面.. – 2013-03-20 13:30:43

+1

谢谢你的回答,但我不想从fb或twitter获得额外字段的信息,用户应该自己填写这些必填字段。 – Bennington 2013-03-20 13:32:48

+0

@Bennington谢谢。我已经更新了答案。也许这可以帮助你。 – Aleks 2013-03-20 13:37:16