0

ユーザーがツイッターに登録するとき、私は彼の名前、場所などをユーザーレコードに追加しようとしています。私は何かをしたいと思うuser.build

こちらがコントローラー。これが起こることです:

    user = User.new
    user.apply_omniauth(omni)
    if user.save
      flash[:notice] = "Logged In!"
      sign_in_and_redirect(:user, user)
    else
      session[:omniauth] = omni.except('extra')
      redirect_to new_user_registration_path
    end

ユーザーが twitter に存在しない場合、ユーザーは登録を完了する登録パスにリダイレクトされます。Twitter の余分なものを、まだ保存されていないユーザー アカウントに追加したいと考えています。user.apply_omniauth(omni)認証テーブルに保存されるため、メソッドでは実行できません。

何か案は?

ありがとう!

4

1 に答える 1

1

メソッドでフラグを作成して、apply_omniauth保存するかどうかを決定できます。

アプリ/モデル/user.rb

# def apply_omniauth(omniauth) => def apply_omniauth(omniauth, save_it)
# apply_omniauth with save it flag
def apply_omniauth(omniauth, save_it = false)
  case omniauth['provider']
  when 'facebook'
    self.apply_facebook(omniauth)
  end
  self.email = omniauth['user_info']['email']
  if email.blank ? build_authentications(omniauth, save_it)
end

#build authentications
def build_authentications(omniauth, save_it = false)
  auth_params = {: provider = > omniauth['provider'],
      : uid = > omniauth['uid'],
      : token = > (omniauth['credentials']['token'] rescue nil)
  }
  if save_it authentications.create!(auth_params)
  else authentications.build(auth_params)
  end
end

#force to save
def apply_omniauth!(omniauth)
  apply_omniauth(omniauth, true)
end
于 2013-08-12T03:35:24.810 に答える