1

http://tutorials.jumpstartlab.com/projects/blogger.html

I am on Jumpstar Blogger 2 tutorial and am stuck in the "paperclip" gem section ... I went through everything, but when I try to upload an image, this below error always pops up. What am I doing wrong here? Thank you, I've followed all instructions in the tutorial. (For more details, I can't really seem to be able to upload an image. All the image attributes seem to be in there, but whenever I upload something and click submit, the flash window does pop up but then Rails Console tells me there's no image URL anywhere and all the image attributes are still all "nil")

ActionController::UrlGenerationError in ArticlesController#create

No route matches {:action=>"show", :controller=>"articles", :id=>nil} missing required keys: [:id]

Extracted source (around line #23):

21   flash.notice = "Article '#{@article.title}' created!"
22 
23   redirect_to article_path(@article)
24 end
25 
26 def destroy

Rails.root: C:/Users/burea1124/Projects/blogger

Application Trace | Framework Trace | Full Trace

app/controllers/articles_controller.rb:23:in `create'

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"03PAzDqQiW33L6P2ZYOmgBbOTqYSjX9Hzd5fFCNBE8Q=",
 "article"=>{"title"=>"Tao Te Ching",
 "body"=>"fjdpsfjf\r\nsf\r\nsdf\r\nsaf\r\ns\r\nsaf\r\nsf\r\n",
 "tag_list"=>"nonfiction",
 "image"=>#<actiondispatch::http::uploadedfile:0x2b79610
 @tempfile="#&lt;Tempfile:C:/Users/BUREA1~1/AppData/Local/Temp/RackMultipart20140523-7512-sncmkw">,
 @original_filename="norinokonoko (2).jpg", @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"article[image]\";
 filename=\"norinokonoko (2).jpg\"\r\nContent-Type: image/jpeg\r\n">},
 "commit"=>"Create Article"}

Controller File

class ArticlesController < ApplicationController
include ArticlesHelper

    def index
        @articles = Article.all
    end

    def show
        @article = Article.find(params[:id])
        @comment = Comment.new
        @comment.article_id = @article.id
    end

    def new 
        @article = Article.new 
    end

    def create
        @article = Article.new(article_params)
        @article.save
        flash.notice = "Article '#{@article.title}' created!"

        redirect_to article_path(@article)
    end

    def destroy
        @article = Article.find(params[:id])
        @article.destroy
        flash.notice = "Article '#{@article.title}' deleted!"

        redirect_to articles_path
    end

    def edit
        @article = Article.find(params[:id])
    end

    def update
        @article = Article.find(params[:id])
        @article.update(article_params)
        flash.notice = "Article '#{@article.title}' updated!"

        redirect_to article_path(@article)
    end

end

Helper Module in the controller for params

module ArticlesHelper
    def article_params
      params.require(:article).permit(:title, :body, :tag_list, :image)
    end
end

rake routes output (sorry for broken indentation. please click here for image of screenshot) http://s12.postimg.org/pzx6o29ct/rakeroutes.png

              Prefix Verb   URI Pattern                                       Controller#Action 
                root GET    /                                                 articles#index
    article_comments GET    /articles/:article_id/comments(.:format)          comments#index
                     POST   /articles/:article_id/comments(.:format)          comments#create  
 new_article_comment GET    /articles/:article_id/comments/new(.:format)      comments#new
edit_article_comment GET    /articles/:article_id/comments/:id/edit(.:format) comments#edit
     article_comment GET    /articles/:article_id/comments/:id(.:format)      comments#show
                     PATCH  /articles/:article_id/comments/:id(.:format)      comments#update
                     PUT    /articles/:article_id/comments/:id(.:format)      comments#update
                     DELETE /articles/:article_id/comments/:id(.:format)      comments#destroy
            articles GET    /articles(.:format)                               articles#index
                     POST   /articles(.:format)                               articles#create
         new_article GET    /articles/new(.:format)                           articles#new
        edit_article GET    /articles/:id/edit(.:format)                      articles#edit
             article GET    /articles/:id(.:format)                           articles#show
                     PATCH  /articles/:id(.:format)                           articles#update
                     PUT    /articles/:id(.:format)                           articles#update
                     DELETE /articles/:id(.:format)                           articles#destroy
                tags GET    /tags(.:format)                                   tags#index
                     POST   /tags(.:format)                                   tags#create
             new_tag GET    /tags/new(.:format)                               tags#new
            edit_tag GET    /tags/:id/edit(.:format)                          tags#edit
                 tag GET    /tags/:id(.:format)                               tags#show
                     PATCH  /tags/:id(.:format)                               tags#update
                     PUT    /tags/:id(.:format)                               tags#update
                     DELETE /tags/:id(.:format)                               tags#destroy

config/routes.rb

Blogger::Application.routes.draw do   
  root to:'articles#index'   
  resources :articles do    
    resources :comments  
  end
  resources :tags    
end

models/article.rb

class Article < ActiveRecord::Base
    has_many :comments
    has_many :taggings
    has_many :tags, through: :taggings
    has_attached_file :image
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]

def tag_list
        self.tags.collect do |tag|
            tag.name
        end.join(", ")
end

def tag_list=(tags_string)
    tag_names = tags_string.split(", ").collect {|s| s.strip.downcase}.uniq
    new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name)}
    self.tags = new_or_found_tags
end

end
Terry Bu
  • 889
  • 1
  • 14
  • 31
  • Post your `create action` of your `controller code` and `rake routes output`. – Pavan May 23 '14 at 18:26
  • 2 things: (1) You should probably wrap `@article.save` with an if-else statement to make sure you are not redirecting to the show action with a nil `@article` object (which is what you'll get if the save fails). (2) Have you restarted your local server after implementing Paperclip? – vich May 23 '14 at 20:40
  • @mmichael thanks I will try that ... Yeah I tried restarting a lot ... I will try #1 – Terry Bu May 23 '14 at 23:18
  • The if-else statement would be to prevent a redirection with a nil object. It's likely that your article isn't being saved. Do you have any validation in your `Article` model? Try using the bang method (`@article.save!`) so you could see if any validations fails in the stack trace. – vich May 24 '14 at 02:44
  • @mmichael thanks sir. yeah I got a new error message when I did @article.save! Validation failed: Image has an extension that does not match its contents "image"=>#, @original_filename="beast.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"article[image]\"; filename=\"beast.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Create Article"} – Terry Bu May 24 '14 at 19:57
  • @mmichael yeah I think the validates_attachment_content_type is failing at the Model level ... Please see this ... thanks much for your help. This is my articles.rb in Models updated now – Terry Bu May 24 '14 at 20:03
  • Answered below...not sure if this will fix your issue but it will at least give you more information. Worst case scenario, you could remove the attachment validation for the sake of continuing the tutorial. – vich May 26 '14 at 15:20

2 Answers2

0

Here's your error:

No route matches {:action=>"show", :controller=>"articles", :id=>nil} missing required keys: [:id]

The problem is you're not passing the id value to your article_path route in the create action:

  @article = Article.new(article_params)
  @article.save
  flash.notice = "Article '#{@article.title}' created!"

  redirect_to article_path(@article)

--

I believe the problem is you've not got an article params method in your controller:

#app/controllers/articles_controller.rb
...
private
def article_params
    params.require(:article).permit(:body, :tag_list, :image)
end

article_params is part of the strong_params functionality of Rails. Because you've not got it in your controller, your active_record object won't save, meaning no id is present for the form. Try adding the code above to the end of your articles controller

--

Btw Tao Te Ching is EPIC

enter image description here

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    lolol thank you Rich! Tao Te Ching is awesome, isn't it?? Turns out this was a Paperclip version issue though ... damn that took me so long to figure out for such a small issue T_T – Terry Bu May 26 '14 at 18:46
0

The validation error from your stack trace: Validation failed: Image has an extension that does not match its contents is most likely being caused by a bug in the latest release of Paperclip. It's currently an open issue in the GitHub repo.

The user @nozpheratu proposed disabling the spoofed type media detector in the Paperclip initializer:

#config/initilizers/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

In case that doesn't fix your issue, you could always downgrade to an earlier version of Paperclip until this bug gets fixed.

Also, check out this other open issue for Paperclip as well as this SO post for additional information.

Hope this helps!

Community
  • 1
  • 1
vich
  • 11,836
  • 13
  • 49
  • 66