Reputation: 3
apologies if this has been asked before. With my rails app, everything works fine in development, but when pushing to Heroku, I run into an issue where it is trying to find a User with ID of '1', but the database is empty. I have migrated, reset, and seeded the db but still get the error from Heroku Logs.
It seems like the problem is in my _userportal partial embedded in the articles/index.html.erb file, which calls the current_user and logged_in? methods from ApplicationController.
It seems like the answer should pop out at me, as it must have to do with the finding User.find(session[:user_id]). Or rather, the database has not been properly reset once pushed to heroku. In the heroku rails console, the User.count is 0, but the app is trying to find a User with ID of '1'. I'm not sure why that is.
From the heroku logs:
2019-05-23T00:02:43.803680+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9] Rendered articles/index.html.erb within layouts/application (8.5ms)
2019-05-23T00:02:43.803865+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9] Completed 500 Internal Server Error in 10ms (ActiveRecord: 2.0ms)
2019-05-23T00:02:43.805254+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9]
2019-05-23T00:02:43.805305+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9] ActiveRecord::RecordNotFound (Couldn't find User with 'id'=1):
2019-05-23T00:02:43.805348+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9]
2019-05-23T00:02:43.805403+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9] app/controllers/application_controller.rb:4:in `current_user'
2019-05-23T00:02:43.805405+00:00 app[web.1]: [fe18170f-fab4-4495-8fe2-b2502762afb9] app/controllers/application_controller.rb:11:in `logged_in?'
ApplicationController.rb:
def current_user
if session[:user_id]
@current_user ||= User.find(session[:user_id])
else
@current_user = nil
end
end
def logged_in?
!!current_user
end
_userportal.html.erb:
<div class="container">
<div class="userportal">
<div class="sessionsection">
<% if logged_in? %>
Logged in as <%= current_user.username %>.
<br>
<br>
<%= button_to 'Log Out', logout_path, method: :delete, class: "btn btn-xs btn-danger" %>
<% else %>
<%= link_to 'Sign Up', signup_path %> or
<%= link_to 'Log In', login_path %>
<% end %>
</div>
<hr>
<div class="actionsection">
<%= link_to 'Create a post', current_user ? post_path : signup_path %>
<hr>
<%= link_to 'See All Posts', blog_path %>
</div>
</div>
</div>
Heroku console:
irb(main):005:0> User.count
(1.7ms) SELECT COUNT(*) FROM "users"
=> 0
Heroku should render current_user to be nil, and not logged in correct?
Thank you to all that help out! Please advise if I should supply more code/info.
UPDATE:
The fix was in the current_user method. Changing:
@current_user ||= User.find(session[:user_id])
to:
@current_user ||= User.find_by(id: session[:user_id])
Worked!
Upvotes: 0
Views: 95
Reputation: 56
Just use find_by(id: val)
so that it will return nil
instead of an exception.
def current_user
if session[:user_id]
@current_user ||= User.find_by(id: session[:user_id])
else
@current_user = nil
end
end
With that current_user
will be nil
if user id in session does not exists.
Upvotes: 1