0

我大致遵循 Railscasts Simple OmniAuth http://railscasts.com/episodes/241-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 http://ruby.railstutorial.org/ruby-on-rails-tutorial-book为我的用户建模,我的网站项目要求用户填写这些字段。

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

session_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

用户.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],但没有奏效。

谢谢。

4

1 回答 1

1

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

用户.rb

validates_confirmation_of :password, :if => :password_required?

def password_required?
  !provider.blank? && super
end
于 2012-05-23T21:31:41.147 回答