0

我无法上传图像,因为我在第 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
4

2 回答 2

0

您要上传什么格式?不是 Paperclip 的问题,而是 ImageMagick 的问题。

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

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


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

gem "cocaine"
于 2012-12-06T08:26:11.203 回答
0

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

脚步:

1 )更改gem "paperclip"gem "paperclip", "~> 3.3.1"gemfile 这将为您的捆绑包安装最新的回形针 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>" }  

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

干杯!

于 2012-12-06T11:00:10.810 回答