2012-12-06 79 views
0

我不能上传图像,因为我在行号3即得到错误在上传方法的 用户控制器,用于@ user.update_attributes(PARAMS [:用户])图片上传错误

我上传方法代码 -

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 "https://stackoverflow.com/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 
+0

你可以发布用户模型,部分has_attached_file:头像。问题来自回形针。 –

+0

嘿,我在问题中添加了用户模型,如果回形针看起来是问题。好心检查。 –

回答

0

最后我发现我的问题的解决方案。

步骤:

1)改变在的Gemfilegem "paperclip"gem "paperclip", "~> 3.3.1"。这将为您的软件包安装最新的回形针宝石。前一个没有。

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>" } 

这可以帮助别人谁遇到这样的问题。

干杯!

+1

您也可以运行“软件包更新”来更新您的gemfiles;) – rorra

+0

宝贵的建议注意。我认为如果我们只提供'gem gemname',那么gemfile将总是安装最新的gem。 –

0

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

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

你现在的问题是,它似乎已经安装了ImageMagick,但没有支持某些图像格式。


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

gem "cocaine" 
+0

我已通过在cmd上发出以下命令来检查ImageMagick对图像格式的支持:convert -list format。为支持读写的ImageMagick找到格式.jpg,.png,.gif等。所以请考虑,因为这不是格式问题。 –

+0

您还通过告诉回形针在哪里找到标识命令进行了正确的设置,右图为: Paperclip.options [:command_path] =“/ usr/local/bin /”(可以是任何其他路径,识别命令) – rorra

+0

我的“/ usr/local/bin /”是空的。所以我明确地检查了文件夹中ImageMagick的位置,并在“usr/include”下找到它。然后,我给了初始化文件的路径为 - Paperclip.options [:command_path] =“/ usr/include/ImageMagick” –