gpt4 book ai didi

ruby-on-rails - 回形针验证导致Rails 3错误

转载 作者:行者123 更新时间:2023-12-02 04:07:04 25 4
gpt4 key购买 nike

我最近将回形针gem安装到了Rails 3应用程序中。我正在尝试允许用户将头像上传到他们的个人资料。我遵循了安装说明,这是我的模型/ View 的外观:

我的“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的编辑表单中(我正在使用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

当我上传小于1mb的图片(jpeg或png)时,一切都会顺利进行;图片已上传,没有错误。如果我尝试上传任何其他类型的文件(MP3,txt等),或者文件/图像大于1mb,则会出现以下错误:
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

最佳答案

can't dump File是Ruby在要求使用File转储Marshal.dump对象时引发的错误。

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

因此问题可能出在验证失败时该怎么做。如果要将 params(包含 File对象)放入 sessionflash对象(由Rails转储)中,这就是您的问题。

关于ruby-on-rails - 回形针验证导致Rails 3错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7087969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com