我使用 Rails 5.1.6;Ruby 版本 2.4.4;和费加罗宝石
在本地我的 RoR-App 似乎可以工作后,我决定将它部署到 Heroku,但是当我尝试进入该网站时,出现应用程序错误。我查看了日志并发现了一个未初始化的常量(NameError)
/app/controllers/Posts/likes_controller.rb:1:in `<top (required)>': uninitialized constant Posts (NameError)
这是我的 likes_controller.rb:
class Posts::LikesController < ApplicationController
before_action :authenticate_user!
before_action :set_post
def show
@post.likes.where(user_id: current_user.id).first_or_create
respond_to do |format|
format.html {redirect_to @post}
format.js
end
end
def create
@post.likes.where(user_id: current_user.id).first_or_create
respond_to do |format|
format.html {redirect_to @post}
format.js
end
end
def destroy
@post = Post.find(params[:post_id])
if @post.present?
@like = Like.where(user_id: current_user.id, post_id: @post.id).first
if @like.present?
@like.destroy
redirect_to post_path(@post), :notice => 'Unliked!'
else
redirect_to post_path(@post), :alert => 'An error prevented you from unliking
this post!'
end
else
redirect_to p, :alert => 'Invalid post!'
end
end
private
def set_post
@post = Post.find(params[:post_id])
end
end
我尝试运行heroku rake db:migrate
以将我的数据库与我的代码匹配,但它仍然给我同样的错误。然后我想也许我的迁移不正确,但在开发模式下控制后它们似乎是正确的。然后我尝试在生产模式下在本地运行 rails ,rails s -e production
这给了我这个错误:
2018-10-28 17:02:24 +0100: Rack app error handling request { GET / }
#<RuntimeError: Missing `secret_key_base` for 'production' environment, set this value
in `config/secrets.yml`>
这对我来说似乎非常接近我的 heroku 日志所说的内容:
2018-10-28T15:50:00.991935+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myapp.herokuapp.com request_id=c081e39f-f4f5-49e6-a7d3-3a72060762cb fwd="131.188.24.12" dyno= connect= service= status=503 bytes= protocol=https
所以我设置了我的 secret_key_baseheroku run rake secret
并将生成的密钥设置为heroku config:set SECRET_KEY_BASE= 'generated key'
之后我控制设置了密钥和我来自 figaro 的所有其他变量heroku config
,一切都是正确的。但我仍然遇到相同的错误/秒。
最后我意识到在 NameError 中,帖子是用大写写的,这似乎不寻常,所以我将其更正为帖子并再次部署,但我仍然得到与以前相同的错误,/controllers/Posts/likes_controller
所以也是用大写写的。
现在我真的不知道我真正的问题是什么。所以如果有人能帮助我就好了。如果您需要更多信息,请告诉我。谢谢
我的错误确实是写作,所以我将文件夹Posts更改为posts。为了将它部署到 heroku,我必须将我的应用程序复制粘贴到一个新目录中,以便我可以使用git init
. 否则它使用我的旧版本并且没有部署文件夹名称的更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句