3

最近、ペーパークリップの宝石をRails3アプリにインストールしました。ユーザーが自分のプロファイルにアバターをアップロードできるようにしようとしています。インストール手順に従いました。モデル/ビューは次のようになります。

私の「user.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']

編集フォームhtmlに以下を追加しました(HAMLbtwを使用しています):

= 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

1MB未満の画像(jpegまたはpng)をアップロードすると、すべてがスムーズに機能します。画像がアップロードされ、エラーはありません。他の種類のファイル(MP3、txtなど)をアップロードしようとした場合、またはファイル/画像が1 MBを超える場合、次のエラーが発生します。

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

すべてが必要でなかったことをお詫びしますが、エラーは少しわかりにくいので、念のために投稿したほうがいいと思いました。

エラーが生成されておらず、アプリケーションが壊れているという検証で何が間違っているのか知りたいのですが。この問題に関する助けをいただければ幸いです。これをお読みいただき、誠にありがとうございました。ご協力をよろしくお願いいたします。ありがとう!

アップデート:

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
4

3 に答える 3

2

can't dump FileFileを使用してオブジェクトをダンプするように求められたときにRubyが発生するエラーMarshal.dumpです。

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

したがって、問題はおそらく検証が失敗したときに何をするかにあります。params(オブジェクトを含むFilesessionまたはflashオブジェクト(Railsによってダンプされる)を配置する場合、これが問題になります。

于 2011-08-20T08:11:49.637 に答える
0

私の場合、ペーパークリップのアタッチメントをクリアするAttachment#clearだけでは十分ではありませんでしたFile

問題は、まだデータが含まれている@queued_for_write属性にあることがわかりました。Attachment

したがって、次の2行で問題が解決しました。

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

これはペーパークリップのバグであり、このコミットで修正されました。

于 2011-11-10T11:54:21.080 に答える
-1

試しましたか

@user.attachment.destroy

応答で@user.errorsを割り当てる前に?

于 2011-09-27T13:16:46.493 に答える