Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

ruby on rails - Skip email validation for omniauth-twitter on devise 2.0

I'm using devise 2.0 and gem omniauth-twitter

The problem is that twitter does not send an email in response, so the model user of my application validates that there is an email and I get the following error in the callback:

Email can't be blank

I have this in my user.rb model:

#config omniauth twitter
def self.find_for_twitter_oauth(access_token, signed_in_resource = nil)
    data = access_token.extra.raw_info
    if user = User.where(:username => data.screen_name).first
        user
    else
        User.create!(:username => data.screen_name, :password => Devise.friendly_token)
    end
end

and I have this in my omniauth_callbacks_controller.rb

  def twitter
    @user = User.find_for_twitter_oauth(request.env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter"
      sign_in_and_redirect @user, :event => :authentication
    else
      # http://stackoverflow.com/questions/7117200/devise-for-twitter-cookie-overflow-error
      session["devise.twitter_data"] = request.env["omniauth.auth"].except('extra')
      redirect_to new_user_registration_url
    end
   end

I want force to user to give a email address for send newsletter, advertirser...etc

How can I fix this problem?

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this to your User model:

def email_required?
  super && provider.blank?
end

You can use an equivalent method for the password:

def password_required?
  super && provider.blank?
end

This should override the field requirements when using Omniauth providers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...