ActiveRecord error: Could not find user without id
I am creating a web application that allows users to log in with Facebook and then redirect them to their feed. If the user does not exist, a new account will be created with Facebook credentials and saved in the Users model.
The problem I am facing is that when I try to redirect back from the Facebook callback to the user profile page, their entry cannot be found (I get an ActiveRecord :: RecordNotFound error in UsersController # show ), regardless of whether the entry already existed or was re-created during the login process.
I am using OmniAuth to authenticate and access the Facebook API, loosely following the conventions outlined in Ryan Bates RailsCast # 360 ( http://railscasts.com/episodes/360-facebook-authentication?autoplay=true )
Here is my user model:
class User < ActiveRecord::Base
def self.from_omniauth(auth)
where(auth.slice(:uid)).first_or_create.tap do |user|
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.img_url = auth.info.image
user.save!
end
end
end
as well as two related controllers,
SessionsController:
class SessionsController < ApplicationController
def new
redirect_to '/auth/facebook'
end
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to user
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
end
and UsersController:
class UsersController < ApplicationController
def show
@user = User.find(params[:uid])
end
end
Trace data and session dump
Also, the trace shows that the error occurs in the 'show' method of the Users controller:
app/controllers/users_controller.rb:4:in `show'
What I don't understand is why the query showing the user uses an id field that is not part of the model as shown in the query parameters:
{"id"=>"1"}
and a session dump:
_csrf_token: "sjym9SfVWrLipH/7Lxn4RCp2Df6kQTNITnhjro2tirI="
session_id: "7d2c6e70bcae6f1ec257fd634a73c44b"
user_id: 1
The most perplexing thing is that when I use the rails console to check the Users database, the following hash is returned, with an extra ID field added that was not part of the original schema:
=> #<User id: 1, uid: "1123581321", name: "Bob Loblaw", oauth_token: "AAAHFZAqhAyeEBADdZCxeCIdFQWjkbQFjfGfJjCiZA8VKSAw5zy...", oauth_expires_at: "2013-04-14 01:15:20", created_at: "2013-02-13 01:19:24", updated_at: "2013-02-13 01:57:18", img_url: "http://graph.facebook.com/1123581321/picture?type=s...">
Scheme:
create_table "users", :force => true do |t|
t.string "uid"
t.string "name"
t.string "oauth_token"
t.datetime "oauth_expires_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "img_url"
end
Routes
The routes are pretty standard, I don't think the problem is there:
resources :users
root to: 'static_pages#home'
match 'auth/:provider/callback', to: 'sessions#create'
match 'auth/failure', to: redirect('/')
match 'signout', to: 'sessions#destroy', as: 'signout'
match '/how_it_works', to: 'static_pages#how_it_works'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
source to share
As rails looks for the user_id stored in your database to access the record. so you need to have it like
@user = User.find(params[:id])
when you show a specific user. rails should find user_id. This is why you are getting this error.
if you look at your session creation method it creates via sessions [: id] = user.id
the user_id column is automatically created when the table is created.
source to share