5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails4.0 x omniauth-facebook で、find_by〜でエラった場合の解決方法

Posted at

以下のサイト参考にomniauth-facebookを導入してみた。
OmniAuthで認証機能を作る

開発環境では平気だったんだけど、Herokuにあげたらエラって、
log見てみたら以下のエラーがあった。

NoMethodError (undefined method `find_by_provider_and_uid' for #<Class:0x007fcb2884a328>):

find_by_provider_and_uidが定義されてないと。
session_controller.rbに書いてある。

  def 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 root_path
  end

以下のサイトにfind_byにハッシュ渡して使えって書いてた。
What's New in Rails 4

下のように書きなおしたら使えるようになった。

  def create
    auth = request.env["omniauth.auth"]
    user = User.find_by(provider: auth["provider"], uid: auth["uid"]) || User.create_with_omniauth(auth)
    session[:user_id] = user.id
    redirect_to root_path
  end

解決!

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?