2016-07-08 20 views
1

WebappにOmniAuth FaceBookのログインに問題があります。 教えてください。このエラーを修正するにはどうすればよいですか?コードを確認してください。ブラウザでNoMethodError Omniauth FacebookのRailsプロジェクト

エラー:文字列

User.rb:

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     x = auth.info.name.to_a 
     user.name = x[0] 
     user.surname = x[1] 
     user.login = auth.info.uid 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 

    end 
    end 

Omniauth_callback_controller.rb:

"名姓" の

未定義のメソッド `to_a」

def facebook 
    if request.env["omniauth.auth"].info.email.blank? 
     redirect_to "https://stackoverflow.com/users/auth/facebook?auth_type=rerequest&scope=email" 
    end 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 
    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     if(@user.surname.nil?) 
     redirect_to new_profile_path 
     else 
     redirect_to my_profile_path 
     end 
    end 
    end 

devise.rb:

config.omniauth :facebook, '*********', '*********', {:client_options => {:ssl => {:verify => false}}} 
+0

Facebookの認証を個別 'auth.info.first_name'と' auth.info.last_name'を提供することができ、ユーザーはそれらをこのようにアクセスするために、二つ以上の名前を持っている場合には、それはより信頼性が高いでしょう。この他のSO [質問](http://stackoverflow.com/questions/33090322/get-first-name-and-last-name-fields-from-facebook-omniauth)を参照してください。 –

答えて

0

名が文字列であり、あなたは、配列

x = auth.info.name.to_a 

使用splitに変換しようとしている代わりに

x = auth.info.name.split 
#=> ['Name', 'Surname'] 
0

あなたはOmniAuthのFIRST_NAMEを利用することができますし、 last_name:

あなたのdevise.rbを更新してください:

config.omniauth :facebook, '*********', '*********', info_fields: 'email, first_name, last_name', {:client_options => {:ssl => {:verify => false}}} 

とあなたのuser.rb:

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 

     user.name = auth.info.first_name 
     user.surname = auth.info.last_name 
     user.login = auth.info.uid 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 

    end 
    end 
+0

私は 'info_fields:' email、first_name、last_name 'を貼り付けたとき、RubyMineは文法エラーについて叫んでこの部分に下線を引いています –

+0

omniauthのドキュメントに従って正しいです: 'config.omniauth:facebook、" APP_ID "、" APP_SECRET "、スコープ: 'email'、info_fields: 'email、name''(ここではhttps://github.com/plataformatec/devise/wiki/OmniAuth:-Overview)あなたはそれを実行しようとしましたか? –

+0

私はfacebookでLoginを押すと、私のlocalhostにリダイレクトされますが、http:// localhost:3000 /#_ = _ –

0

あなたuser.rbこの

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     x = auth.info.name.split 
     if x.count > 1 
     user.name = x[0] 
     user.surname = x[1] 
     else 
     user.name = x[0] 
     end 
     user.login = auth.info.uid 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 
    end 
end 

ようにする必要がありますが、名前をすることができたので、私は、単一のフィールドに格納することをお勧め2つ以上の単語があるので、名前と姓を特定するのが難しくなります。

this link should also help

関連する問題