在Rails 3中導致錯誤的paperclip驗證

[英]paperclip validations causing Error in Rails 3


I've recently installed the paperclip gem to my Rails 3 app. I am trying to allow users to upload an avatar to their profile. I followed the install instructions and here is what my models/views look like:

我最近在我的Rails 3應用上安裝了paperclip gem。我試圖讓用戶上傳一個頭像到他們的個人資料中。我遵循了安裝說明,以下是我的模型/視圖:

My 'user.rb' model has the following code:

我的用戶。rb的模型有以下代碼:

has_attached_file :avatar, 
                  :styles => { :small => "70x70>"},
                  :url  => "/users/:attachment/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/users/:attachment/:id/:style/:basename.:extension"

validates_attachment_size :avatar, :less_than => 1.megabytes
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png']

I have added the following to the edit form html (I'm using HAML btw):

我在編輯表單html中添加了以下內容(我正在使用HAML btw):

= form_for (@user || User.new), :html => { :multipart => true } do |f|
...
.profile_picture.text_field
  = image_tag current_profile.avatar.url(:small)
  %br
  = f.file_field :avatar

When I upload a picture (jpeg or png) that is under 1mb everything works smoothly; the image is uploaded, and there are no errors. If I try to upload any other type of file (MP3, txt, whatever) or the file/image is greater than 1mb rails gives me the following error:

當我上傳低於1mb的圖片(jpeg或png)時,一切都很順利;圖像被上傳,沒有錯誤。如果我嘗試上傳任何其他類型的文件(MP3, txt,等等)或文件/圖像大於1mb rails,則會出現以下錯誤:

TypeError in UsersController#update

can't dump File

Request

Parameters:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"LaiYjEEfgsE8JzzLsfkzk6TK8D4uxzIo5ASlu6ax2rY=",
 "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x000001053f0bf0 @original_filename="GT1_22HS_1_std.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"GT1_22HS_1_std.jpg\"\r\nContent-Type: image/jpeg\r\n",
 @tempfile=#<File:/var/folders/Ud/Udv4OlryEzWrHedR8pIe1E+++TI/-Tmp-/RackMultipart20110817-17075-1ikqcc0>>,
 "first_name"=>"First",
 "last_name"=>"Last",
 "country"=>"United States",
 "state"=>"California",
 "city"=>"City",
 "date_of_birth(1i)"=>"2011",
 "date_of_birth(2i)"=>"7",
 "date_of_birth(3i)"=>"12",
 "account_attributes"=>{"email"=>"email@email.com",
 "id"=>"6",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Save & Continue",
 "id"=>"2"}

I apologize if all of that was not required, but the error is a little non-descriptive, so I figured it's better to post in just in case.

如果所有這些都不是必需的,我很抱歉,但是這個錯誤有點非描述性,所以我認為最好還是貼出來以防萬一。

I would like to know what I'm doing wrong with my validations that errors are not being generated, and the application is breaking. Any help on this matter would be greatly appreciated. I sincerely thank everyone who took the time to read this, and I kindly appreciate any help! Thanks!

我想知道我的驗證有什么問題:錯誤沒有生成,應用程序正在崩潰。在這個問題上任何幫助都將非常感謝。我真誠地感謝每一個花時間閱讀這篇文章的人,也非常感謝任何幫助!謝謝!

UPDATE:

更新:

update method form user_controller.rb:

  def update
    session[:user_params] ||= {}
    session[:user_params].deep_merge!(params[:user]) if params[:user].present?

    @user.attributes = session[:user_params]

    respond_to do |format|
      if @user.save
        session[:user_params] = nil
        sign_in(@user.account, :bypass => true)
        format.html { redirect_to(root_url, :notice => 'User was successfully updated.') }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "edit", :layout => "userhome" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

3 个解决方案

#1


2  

can't dump File is the error Ruby raises when it is asked to dump a File object using Marshal.dump.

不能轉儲文件是當Ruby被請求使用Marshal.dump轉儲文件對象時引發的錯誤。

Marshal.dump(File.new(".gemrc")) # => TypeError: can't dump File

So the problem lies probably in what you do when the validation fails. If you're putting the params (which contains a File object) in the session or the flash objects (which are dumped by Rails), this is your problem.

所以問題可能在於當驗證失敗時你該怎么做。如果您將params(其中包含一個文件對象)放入會話或flash對象(被Rails轉儲),那么這就是您的問題。

#2


0  

Did you try

你試過

@user.attachment.destroy

before assigning @user.errors in your response?

分配@user之前。錯誤在你的反應?

#3


0  

The problem in my case that clearing out the paperclip attachment with Attachment#clear was not enough because it still wanted to save some File.

在我的例子中,清除帶有附件#clear的paperclip附件的問題是不夠的,因為它仍然想保存一些文件。

I've found out that the problem was with the @queued_for_write attribute in Attachment which still contained the data.

我發現問題是附件中的@queued_for_write屬性仍然包含數據。

So the following two lines solved my problem:

下面兩行解決了我的問題:

unless @model.valid?
  @model.image.clear
  @model.image.queued_for_write.clear
end

This was a paperclip bug and was corrected in this commit.

這是一個paperclip錯誤,並在此提交中更正。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/08/17/729a7778b590e0a180dbd75369d3d0bd.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com