由于验证,简单的OmniAuth-Twitter无法创建新用户 [英] Simple OmniAuth-Twitter failing to create new user because of validation

查看:231
本文介绍了由于验证,简单的OmniAuth-Twitter无法创建新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我松散地关注Railscasts Simple OmniAuth http://railscasts.com/episodes/241-simple -omniauth 创建我的Twitter登录名.当我尝试通过Twitter登录并创建新用户时,收到以下消息:

I am loosely following Railscasts Simple OmniAuth http://railscasts.com/episodes/241-simple-omniauth to create my Twitter login. When I try to sign in and create a new user through twitter, I get the following message:

Validation failed: Password can't be blank, Name is not valid., Email can't be blank, Email is not valid.

如果我点击刷新按钮,则会出现以下错误:

If I hit the refresh button, I get the following error:

OAuth::Unauthorized 401 Unauthorized

我已经将回调URL设置为http://127.0.0.1:3000/auth/twitter/callback,但仍然收到该消息.我正在localhost上进行测试.

I've already set my callback URL to http://127.0.0.1:3000/auth/twitter/callback but I still get that message. I'm testing on localhost.

我以Hartl的railstutorial http://ruby.railstutorial.org为用户建模/ruby​​-on-rails-tutorial-book ,而我的网站项目要求用户填写这些字段.

I modeled my users after Hartl's railstutorial http://ruby.railstutorial.org/ruby-on-rails-tutorial-book and my website project requires that users have those fields filled in.

与铁路广播不同,我创建了一个新的方法来处理omniauth登录:

Unlike the railscasts, I created a new method to handle omniauth logins:

sessions_controller.rb

sessions_controller.rb

def omniauth_create
  auth = request.env["omniauth.auth"]
  user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || 
         User.create_with_omniauth(auth)
  session[:user_id] = user.id
  redirect_to user
end

user.rb

def self.create_with_omniauth(auth)
  create! do |user|
    user.provider = auth["provider"]
    user.uid = auth["uid"]
    user.name = auth["info"]["name"]
  end
end

问题:如何绕过验证?

到目前为止,我已经尝试过使用skip_before_filter :authenticate, :only => [:omniauth_create]了,但是那没用.

So far, I've tried using skip_before_filter :authenticate, :only => [:omniauth_create], but that didn't work.

谢谢.

推荐答案

有两种跳过验证的方法.您可以通过将:validate => false传递给create方法(通常是一个坏主意)来跳过它们,或者可以将验证修改为以下内容:

There are two ways to skip the validations. Either you can skip them all by passing :validate => false to the create method (usually A Bad Idea), or you can modify your validations to something like the following:

user.rb

validates_confirmation_of :password, :if => :password_required?

def password_required?
  !provider.blank? && super
end

这篇关于由于验证,简单的OmniAuth-Twitter无法创建新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆