【问题标题】:Issue deploying Rails 5 app using Devise to Heroku and to Github使用 Devise 将 Rails 5 应用程序部署到 Heroku 和 Github 的问题
【发布时间】:2019-07-30 06:17:45
【问题描述】:

我的这个应用程序在我的本地 Windows 系统上运行良好。这是我被分配的使用 Devise 在 Rails 5 上进行的基本 CRUD 练习。我试图推送到 Heroku 并收到一个错误,指出它不支持 sqlite,所以我将其移至开发,仅在 Michael Hartl 的教程中进行测试。它终于奏效了,它住在:https://sheltered-brook-53935.herokuapp.com/

但是当我尝试访问“注册”或“登录”路由时,我收到 Heroku 错误,提示出现问题。

运行 git push heroku master 后的终端输出 https://gist.github.com/garosan/c3246b5bb9ff347ca0e9ea0e96ad413b

Heroku 日志 https://gist.github.com/garosan/3c3ccca6c4f6a8a0c64f829f85341470

此外,我需要共享 repo 以供某人检查我的工作,我将其推送到 https://github.com/garosan/rails-habits-tracker,但是当我下载 zip 并尝试运行它时,我得到了可怕的 ActiveRecord::PendingMigrationError。这可以通过执行 rails db:migrate 来解决,但是检查我的仓库的人不需要这样做。关于如何修复它的任何想法?

【问题讨论】:

  • Heroku 部署在运行后工作:heroku run rails db:migrate 但我仍然可以让我的 repo 在克隆后顺利运行,很明显我的迁移存在问题!奇怪的是,如果我运行 git status 我没有任何待添加或提交的内容,所以我猜这与推送到 Github 时被 gitignored 的文件有关。
  • 你能添加来自HabitsController#index的代码吗?

标签: heroku devise ruby-on-rails-5


【解决方案1】:

我可以看到您的代码,我认为缺少current_user,如果您使用的是设计,请确保您在控制器中检查before_action :authenticate_user!,因此如果用户不存在,它将重定向到登录页面。要在索引操作中调试注释您的代码,并检查 puts current_user.inspect 是否会出现。

class HabitsController < ApplicationController
   before_action :authenticate_user!

   def index
     @habits = Habit.where(user_id:current_user.id)
     #current_user is not present in your case so it is throwing nil class error 
   end
end

您可以在这里查看更多选项Devise controller helpers

【讨论】:

  • 谢谢,这实际上不是问题,但我已经提交了代码。这是我第一次接触 Rails,所以只是继续学习。
猜你喜欢
  • 2014-04-07
  • 2011-10-03
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 2021-10-17
  • 2016-01-06
  • 2019-04-20
相关资源
最近更新 更多