Ruby on rails Simple OmniAuth Twitter由于验证而无法创建新用户

Ruby on rails Simple OmniAuth Twitter由于验证而无法创建新用户,ruby-on-rails,ruby-on-rails-3,omniauth,twitter-oauth,Ruby On Rails,Ruby On Rails 3,Omniauth,Twitter Oauth,我松散地遵循Railscasts Simple OmniAuth创建我的Twitter登录。当我尝试通过twitter登录并创建新用户时,我收到以下消息: Validation failed: Password can't be blank, Name is not valid., Email can't be blank, Email is not valid. 如果我点击刷新按钮,我会得到以下错误: OAuth::Unauthorized 401 Unauthorized 我已经将回调U

我松散地遵循Railscasts Simple OmniAuth创建我的Twitter登录。当我尝试通过twitter登录并创建新用户时,我收到以下消息:

Validation failed: Password can't be blank, Name is not valid., Email can't be blank, Email is not valid.
如果我点击刷新按钮,我会得到以下错误:

OAuth::Unauthorized 401 Unauthorized
我已经将回调URL设置为
http://127.0.0.1:3000/auth/twitter/callback
但我仍然得到了这个信息。我正在本地主机上测试

我按照Hartl的railstutorial为我的用户建模,我的网站项目要求用户填写这些字段

与railscasts不同,我创建了一个新方法来处理omniauth登录:

会话\u 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
validates_confirmation_of :password, :if => :password_required?

def password_required?
  !provider.blank? && super
end
问题:如何绕过验证?

到目前为止,我已经尝试使用
skip\u-before\u-filter:authenticate,:only=>[:omniauth\u-create]
,但没有成功


谢谢。

有两种方法可以跳过验证。您可以通过将
:validate=>false
传递给create方法(通常是个坏主意)来跳过所有验证,也可以将验证修改为如下内容:

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
validates_confirmation_of :password, :if => :password_required?

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

看起来姓名和电子邮件也没有通过验证。如果我遵循您的方法,这是否意味着我必须再编写两个方法来测试是否需要姓名和电子邮件?是的,这是正确的,您将以与上述类似的方式再创建两个方法。不过,您可能希望确保这是您真正想要的-没有姓名和电子邮件确实限制了您可以构建的涉及用户数据的功能。这是我第一次从外部来源获取数据。我有没有办法将“我的名字”字段分配给用户的twitter名称,将“我的电子邮件”字段分配给用户的twitter电子邮件?请记住,不同的服务提供不同的信息。例如,IIRC twitter API提供了一个名称字段(而不是名字和姓氏),并且不提供电子邮件。还有一条建议:不要使用localhost或127.0.0.1作为回调url,因为大多数oauth提供程序不会路由到该url。相反,使用lvh.me-它总是转到localhost(分配给127.0.0.1的IP)。