Heroku上的Rails应用导致数据库迁移错误(找不到用户ID)

时间:2019-05-23 00:31:55

标签: ruby-on-rails heroku

很抱歉,如果以前曾问过这个问题。使用我的rails应用程序,一切都可以在开发中正常工作,但是当推送到Heroku时,我遇到了一个问题,即它试图查找ID为'1'的User,但是数据库为空。我已经迁移,重置数据库并植入了种子,但是仍然从Heroku Logs中得到错误。

问题似乎出在我的_userportal部分中,该部分嵌入在articles / index.html.erb文件中,该文件调用current_user和logging_in?来自ApplicationController的方法。

似乎答案应该在我眼前,因为它必须与查找User.find(session [:user_id])有关。或者更确切地说,一旦推送到heroku,就没有正确重置数据库。在heroku rails控制台中,User.count为0,但应用程序尝试查找ID为“ 1”的用户。我不确定为什么会这样。

从heroku日志中:

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控制台:

irb(main):005:0> User.count
   (1.7ms)  SELECT COUNT(*) FROM "users"
=> 0

Heroku应该将current_user设置为nil,并且未正确登录吗?

感谢您提供的所有帮助!请告知我是否应该提供更多代码/信息。

更新:

此修复程序在current_user方法中。更改:

@current_user ||= User.find(session[:user_id])

收件人:

@current_user ||= User.find_by(id: session[:user_id])

工作!

1 个答案:

答案 0 :(得分:1)

只需使用const Element = styled.span` font-size: 13px; font-weight: 500; `; // These styles are sample only. Actual styles depend on a "variant" prop. // I did not show it here since it doesn't have to do with the question I'm asking. export default ({ tag }) => (<Element as={tag} />); ,以便它将返回find_by(id: val)而不是异常。

nil

如果会话中的用户ID不存在,则def current_user if session[:user_id] @current_user ||= User.find_by(id: session[:user_id]) else @current_user = nil end end 将是current_user