3

I have an app with a basic blog structure. Creating new blogs works perfectly on localhost, but when I try to create a new blog on Heroku I get the following error in my logs:

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

My blogs#create method is decently simple:

 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

And I have the following permissions:

 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

I'm not sure where things are going off the rails (no pun intended). I did see this question where the issue was a database issue. In which case, the only recent change I've made would be in my blogs#show method...even though I have no idea how that would prevent a blog from even saving in the database (which it doesn't):

  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

Can anyone see where I'm going wrong?

Liz
  • 1,369
  • 2
  • 26
  • 61
  • hard to tell what went wrong, `undefined method []= for nil:NilClass` suggests assignment on nil object. What line is `app/controllers/blogs_controller.rb:48`? – kasperite Jul 12 '18 at 00:00
  • @kasperite line 48 is my `@blog = Blog.new(blog_params)`, which is why I included my params in the OP. Sorry that wasn't clearer... – Liz Jul 12 '18 at 00:40
  • I'm guessing it's something to do with `image` then since you said it works fine locally? – kasperite Jul 12 '18 at 04:23
  • @kasperite Oh no. I just upgraded to rails 5 and the image uploads with paperclip. Is it THAT depreciated already? – Liz Jul 12 '18 at 04:25
  • the README said it is but I don't know what your case is like, you may want to consult the migration docs :) – kasperite Jul 12 '18 at 04:28
  • 1
    also `if current_user.id = 1` is wrong, it should be `if current_user.id == 1` – Ravi Mariya Jul 12 '18 at 05:23
  • @RaviMariya Thank you for pointing this out! I did fix that issue, but unfortunately it doesn't fix the main issue. – Liz Jul 14 '18 at 03:20
  • What are the params being passed? Can you add more logs from before and after the request? – Kartikey Tanna Jul 14 '18 at 15:10
  • @KartikeyTanna unfortunately I think kasperite hit on the right idea that paperclip was not valid with rails 5.2. Tried performing the same action with ActiveStorage but hit on this problem: https://stackoverflow.com/questions/51314520/console-error-trying-to-do-direct-uploads-with-rails-activestorage-and-amazon-aw – Liz Jul 14 '18 at 15:21
  • Without the image is it working fine? – Kartikey Tanna Jul 14 '18 at 16:51
  • You could start a `heroku run rails console -a your_app` and try to create a blog manually in the console to debug it better. Are you sure you are not missing a migration? – Derek Jul 14 '18 at 17:11
  • @KartikeyTanna No, I get the same error without an image. – Liz Jul 14 '18 at 20:15
  • @Derek no, it's actually messed up to the point where a console won't even open. – Liz Jul 14 '18 at 20:16
  • you might also try `heroku logs --tail` and then post the output when trying to save a post, after looking at this question and then coming back to it I feel like there is just not enough information here to solve. – Rockwell Rice Jul 14 '18 at 20:22
  • Wow, if you can't even get a rails console working something is seriously wrong. Push up the branch again and see if you have any errors. – Derek Jul 15 '18 at 03:45

2 Answers2

1

From the logs and the fact it's working fine on your localhost, it's probably an error to host your tmp image file.

You should take a look at these articles:

Edit:

I just saw you get the same error without an image, but don't you set any default image ?

-> Could you post error logs without uploaded image ?

Edit2:

I went on your website, I updated an image on an existing blog post and it works so it's probably not linked to the image system.

Edit3:

After some tests on your websites, it's the tag_list field which is wrong: you can create some new blog post without tags but as soon as you insert some tags an error is raised.

Ps: Sorry I did not managed to get my tests deleted without routes

XavM
  • 863
  • 9
  • 22
0

Have you checked your migrations on Heroku?