2012-12-06 80 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」 –