Paperclip-Rails的图像上传错误

时间:2012-12-06 08:20:17

标签: ruby-on-rails image file-upload paperclip

我无法上传图片,因为我在上传方法中遇到错误 第3行的用户控制器,即 @ user.update_attributes(params [:user])

我的上传方法代码 -

  def upload  
     @user = User.find_by_id(current_user.id)
   if @user.update_attributes(params[:user])  
    flash[:notice] = "Successfully uploaded Image."  
    if params[:user][:avatar].blank?  

      redirect_to @user  
    else  

      render :action => 'crop'  
    end  
  else  
    render :action => 'new'  
  end  
end  

我的表单 -

   <%= form_for(:user, :url => {:action => 'upload'}, 
  :html => {:multipart => true}) do |f| %>

   <div class="inputs">

   <p>
     <%= f.label :avatar %>
     <%= f.file_field :avatar %>
   </p>
 </div>
   <div class="actions">
    <%= f.submit "Upload" %>
   </div>
 <% end %> 

我的用户模型 -

    class User < ActiveRecord::Base
      devise :database_authenticatable, :registerable,:recoverable, 
      :rememberable,  :trackable, :validatable

      attr_accessible :email, :password, :password_confirmation, :remember_me, :avatar

      has_attached_file :avatar, 
      :styles  => { :small => "100x100#", :large  => "500x500>" }  

    end

我在命令提示符下的输出 -

    identify: unable to open image `file':  @ error/blob.c/OpenBlob/2498.
    identify: no decode delegate for this image format `file' @ error/constitute.c     /ReadImage/532.

 Started POST "/users/upload" for 127.0.0.1 at 2012-12-06 13:28:17 +0530
 Processing by UsersController#upload as HTML
 Parameters: {"utf8"=>"✓",  "authenticity_token"=>"mUPao835NCC8qXO553RJH50NWLhzQk03Z2G3Y0LwMYE=", 
"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0xa20a418 @original_filename="freedomking.jpg",  @content_type="image/jpeg", @headers="Content-
 Disposition: form-data; name=\"user[avatar]\";  filename=\"freedomking.jpg            
 \"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20121206-3368-127uzt5>>}, "commit"=>"Upload"}
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(1.1ms)  begin transaction
[paperclip] An error was received while processing:     
#<Paperclip::NotIdentifiedByImageMagickError: /tmp/stream20121206-7054-b
teogf.jpg is not recognized by the 'identify' command.>
[paperclip] An error was received while processing:    
#<Paperclip::NotIdentifiedByImageMagickError: /tmp/stream20121206-7054-b
teogf.jpg is not recognized by the 'identify' command.>
  (0.1ms)  rollback transaction

3 个答案:

答案 0 :(得分:1)

文件上传是Rails中的简单过程,

这是一步一步的过程
1. gem install paperclip或在您的gem文件中添加gem 'paperclip'
2.捆绑安装。
3.通过迁移将文件添加到模型中
在您的迁移文件中

def change
  add_attachment :table_name, :fieldname
end
  1. rails db:migrate
  2. 在您的模型中
class Dish < ApplicationRecord
    has_attached_file :image
    has_and_belongs_to_many :restaurants

    has_attached_file :image, styles: {
      thumb: '10x10>',
      medium: '50x50>',
      processors: [:thumbnail]}, default_url: "/images/:style/missing.png" 

      validates_attachment :image, :content_type => {:content_type => 
      %w(image/jpeg image/jpg image/png application/pdf)},

      attachment_size: { less_than: 5.megabytes }

 end

就这样,现在检查您的图片是否成功上传

答案 1 :(得分:0)

您尝试上传哪种格式?不是Paperclip的问题,而是ImageMagick的问题。

Paperclip使用ImageMagick通过命令标识处理图像。为了能够使用.jpg,.png,.gif等,您必须安装ImageMagick并支持这些格式和其他格式。

您现在遇到的问题是,您似乎安装了ImageMagick,但不支持某些图像格式。


好的,第二次尝试:添加宝石可卡因,当高时,导轨通常效果更好:P

gem "cocaine"

答案 2 :(得分:0)

最后我找到了问题的解决方案。

步骤:

1)在 gemfile 中将gem "paperclip"更改为gem "paperclip", "~> 3.3.1"。这将为您的捆绑包安装最新的paperclip gem。前一个没有。

2)更改用户模型的has_attached_file -

has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment /:id/:style /:filename",
:url =>  "/system/:attachment/:id/:style/:filename", 
:styles  => { :small => "100x100#", :large => "500x500>" }  

这可能会帮助遇到此类问题的人。

干杯!