我有带有 gem devise + gem omniauthn(多个提供者)的 Rails 应用程序。我跟着本教程:http ://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/ (应用示例:https ://github.com/klebershimabuku/fb_auth_demo )
用户型号:
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
认证模型:
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 注册?