我有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註冊?
你有沒有發現,如何做到這一點? – Cyzanfar 2016-10-20 04:12:12