我有以下代码:
用户.rb:
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
# Add an error message when email is already taken
user.email = auth["info"]["email"]
user.password = user.password_confirmation = SecureRandom.urlsafe_base64(n=6)
end
end
session_controller.rb:
def omniauth_create
auth = request.env["omniauth.auth"]
user = User.from_omniauth(env["omniauth.auth"])
if user.save
sign_in user
redirect_back_or user
else
#session[:omniauth] = request.env['omniauth.auth'].except('extra')
redirect_to signup_path
flash.now[:error] = 'Worked!'
end
end
现在此代码不起作用,因为create! do
在 User 模型中保存了用户,并且在执行到达控制器并通过 if 语句之前引发了错误。所以我想new! do
改用,但得到了这个:
undefined method `new!' for #<Class:0xb5aaf04>
为什么“创造!做”没有“新!做”?
(我采取了错误的方法来解决这个问题?如果是这样,我可以应用哪些其他解决方案?)