ruby on rails でpaperclipを使って画像upload機能をつけようとしているのですが、
画像upload時に、
Paperclip::Errors::NotIdentifiedByImageMagickError
と出て上手く動作しません。
githubのreadmeや
https://github.com/thoughtbot/paperclip
下記記事などを参考にコードを書いているのですが、何度やっても同じエラーが出てしまいます。
http://ruby-rails.hatenadiary.com/entry/20140716/1405443484
新たにアプリを作成しても、paperclipが標準搭載されたオープンソースのアプリをインストールしてみても画像upload時に同じエラーが出ます。
config/enviroments/deveropment.rb/には、
Paperclip.options[:command_path] = "/usr/local/bin/"
を入れて、
それぞれのファイルは下記のように書いています。
原因の分かる方がいらっしゃればご教示頂ければ幸いです。
何卒よろしくお願い致します。
db/migrate/20141030103816_add_attachment_avatar_to_items.rb
lang
1class AddAttachmentAvatarToItems < ActiveRecord::Migration 2 def self.up 3 change_table :items do |t| 4 t.attachment :avatar 5 end 6 end 7 8 def self.down 9 remove_attachment :items, :avatar 10 end 11end 12 13
app/model/item.rb
lang
1 2class Item < ActiveRecord::Base 3 4has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" 5validates_attachment :avatar, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] } 6 7end
app/views/items/_form
lang
1 2<%= form_for @item, :url => items_path, :html => { :multipart => true } do |f| %> 3 <% if @item.errors.any? %> 4 <div id="error_explanation"> 5 <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2> 6 7 <ul> 8 <% @item.errors.full_messages.each do |message| %> 9 <li><%= message %></li> 10 <% end %> 11 </ul> 12 </div> 13 <% end %> 14 15 <div class="field"> 16 <%= f.label :name %><br> 17 <%= f.text_field :name %> 18 </div> 19 <div class="field"> 20 <%= f.label :price %><br> 21 <%= f.number_field :price %> 22 </div> 23 24 <%= f.file_field :avatar %> 25 <div class="actions"> 26 27 <%= f.submit %> 28 </div> 29<% end %> 30 31 32 33
app/controllers/items_controller
lang
1class ItemsController < ApplicationController 2 before_action :set_item, only: [:show, :edit, :update, :destroy] 3 4 # GET /items 5 # GET /items.json 6 def index 7 @items = Item.all 8 end 9 10 # GET /items/1 11 # GET /items/1.json 12 def show 13 end 14 15 # GET /items/new 16 def new 17 @item = Item.new 18 end 19 20 def new2 21 @item =Item.new 22 23 end 24 25 # GET /items/1/edit 26 def edit 27 end 28 29 # POST /items 30 # POST /items.json 31 def create 32 @item = Item.create(item_params) 33 34 35 36 respond_to do |format| 37 if @item.save 38 format.html { redirect_to @item, notice: 'Item was successfully created.' } 39 format.json { render :show, status: :created, location: @item } 40 else 41 format.html { render :new } 42 format.json { render json: @item.errors, status: :unprocessable_entity } 43 end 44 end 45 end 46 47 # PATCH/PUT /items/1 48 # PATCH/PUT /items/1.json 49 def update 50 respond_to do |format| 51 if @item.update(item_params) 52 format.html { redirect_to @item, notice: 'Item was successfully updated.' } 53 format.json { render :show, status: :ok, location: @item } 54 else 55 format.html { render :edit } 56 format.json { render json: @item.errors, status: :unprocessable_entity } 57 end 58 end 59 end 60 61 # DELETE /items/1 62 # DELETE /items/1.json 63 def destroy 64 @item.destroy 65 respond_to do |format| 66 format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' } 67 format.json { head :no_content } 68 end 69 end 70 71 private 72 # Use callbacks to share common setup or constraints between actions. 73 def set_item 74 @item = Item.find(params[:id]) 75 end 76 77 # Never trust parameters from the scary internet, only allow the white list through. 78 def item_params 79 params.require(:item).permit(:name, :price, :avatar) #paperclip使う時は:avatarを追加 80 end 81end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。