ruby-on-rails - Rails NoMethodError for Blog#create on Heroku,但不是 Localhost

标签 ruby-on-rails heroku

我有一个具有基本博客结构的应用程序。在 localhost 上创建新博客非常有效,但是当我尝试在 Heroku 上创建新博客时,我的日志中出现以下错误:

2018-07-11T21:20:01.863867+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] Command :: file -b --mime '/tmp/819c55b783715f61a2656207b4852b5c20180711-4-140ohfr.jpg'
2018-07-11T21:20:01.872443+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] Completed 500 Internal Server Error in 38ms (ActiveRecord: 0.0ms)
2018-07-11T21:20:01.873180+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0]
2018-07-11T21:20:01.873253+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] NoMethodError (undefined method `[]=' for nil:NilClass):
2018-07-11T21:20:01.873285+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0]
2018-07-11T21:20:01.873329+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] app/controllers/blogs_controller.rb:48:in `create'
2018-07-11T21:20:01.874027+00:00 app[web.1]: 10.101.219.132 - - [11/Jul/2018:21:19:56 UTC] "POST /blogs HTTP/1.1" 500 1958
2018-07-11T21:20:01.874063+00:00 app[web.1]: http://www.linchpinrealty.com/blogs/new -> /blogs
2018-07-11T21:20:01.874816+00:00 heroku[router]: at=info method=POST path="/blogs" host=www.linchpinrealty.com request_id=2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0 fwd="68.225.227.137" dyno=web.1 connect=0ms service=5463ms status=500 bytes=2235 protocol=http

我的blogs#create 方法非常简单:

 def create
    @pillars = Pillar.all
    @blog = Blog.new(blog_params)
    if current_user.id = 1
      @blog.user_id = 2
    else
      @blog.user = current_user
    end

    if @blog.save
      redirect_to @blog, notice: 'Blog was successfully created.'
    else
      render :new
    end
  end

我有以下权限:

 private
    # Use callbacks to share common setup or constraints between actions.
    def set_blog
      @blog = Blog.friendly.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def blog_params
      params.require(:blog).permit(:title, :teaser, :body, :user_id, :image, :tag_list, :link_text, :link_filename, :pillars_id)
    end

我不确定哪里出了问题(没有双关语意)。我确实看到 this 问题是数据库问题。在这种情况下,我最近所做的唯一更改是在我的 blogs#show 方法中......尽管我不知道这将如何阻止博客甚至保存在数据库中(这它没有):

  def show
    @pillars = Pillar.all
    @pillar = Pillar.find_by(id: @blog.pillars_id)
    @related = Blog.where(pillars_id: @blog.pillars_id).where.not(id: @blog.id).limit(4)
    @comment = @blog.comments.build
    @comments = Comment.where(blog_id: @blog.id, approved: true)
    if current_user
      @user = current_user
    end
  end

谁能看出我哪里出错了?

最佳答案

从日志和它在您的本地主机上运行良好的事实来看,托管您的 tmp 图像文件可能是一个错误。

你应该看看这些文章:

编辑:

我刚刚看到你在没有图像的情况下得到同样的错误,但是你没有设置任何默认图像吗?

-> 你能在没有上传图片的情况下发布错误日志吗?

编辑2:

我访问了您的网站,我更新了现有博客文章中的图片并且它有效,所以它可能没有链接到图片系统。

编辑 3:

在您的网站上进行一些测试后,tag_list 字段是错误的:您可以创建一些没有标签的新博文,但一旦插入一些标签就会引发错误。

Ps: 对不起,我没有设法删除没有路由的测试

关于ruby-on-rails - Rails NoMethodError for Blog#create on Heroku,但不是 Localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51294905/

相关文章:

ruby-on-rails - Rails 茧 : Removing a child without removing the parent (Deeply Nested Forms)

heroku - robots.txt 和禁止绝对路径 URL

ruby-on-rails - 使用 heroku 和 sidekiq 时,我将 ENV ["WEB_CONCURRENCY"] 设置为什么

ruby-on-rails - 有没有好的 HAML -> ERB/H​​TML 转换器?

ruby-on-rails - 如何在 Rails 应用程序中设置复合键

ruby-on-rails - 使用 Garb 获取 Google Analytics 配置文件

ruby-on-rails - 尝试显示错误消息,但遇到: undefined method `errors' for nil:NilClass

ruby-on-rails - 如何从 RVM 配置文件指定要在 Heroku 上使用的 ruby​​ 版本

python - × 尝试在 heroku 中安装软件包时遇到错误

ruby-on-rails - 不使用 git 将 Rails 应用程序部署到 Heroku?