首页 > 解决方案 > Rails NoMethodError 用于 Heroku 上的 Blog#create,但不是 Localhost

问题描述

我有一个具有基本博客结构的应用程序。创建新博客在 上完美运行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

我不确定事情在哪里出轨(不是双关语)。我确实看到了这个问题,问题是数据库问题。在这种情况下,我最近所做的唯一更改是在我的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

谁能看到我哪里出错了?

标签: ruby-on-railsheroku

解决方案


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

你应该看看这些文章:

编辑:

我刚看到你get the same error without an image,但你没有设置任何默认图像吗?

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

编辑2:

我访问了您的网站,更新了现有博客文章中的图像,并且它可以正常工作,因此它可能未链接到图像系统。

编辑3:

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

Ps:对不起,我没有设法在没有路线的情况下删除我的测试


推荐阅读