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="#<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