Friday, July 20, 2018

Rails NoMethodError for Blog#create on Heroku, but not Localhost

Leave a Comment

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?

2 Answers

Answers 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

Answers 2

Have you checked your migrations on Heroku?

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment