Rails 3.1 +回形针 + jQuery 文件上传
我一直在寻找一种使用 Paperclip 和 jQuery 文件上传。查看 jQuery fileupload 页面上的教程,我设置了系统但我找不到一种方法让回形针处理上传的文件。
这就是我所拥有的(简而言之):
# model
has_attached_file :photo ...
# view
= form_for @user, :html => {:multipart => true} do |f|
f.file_field :photo, :multiple => true
f.submit
# controller
def create
@user = User.new params[:user]
if @user.save
render :json => [...]
end
如果我检查提交的数据,我会在 params[:user] 和 params[:user][:photo] 中获取所有用户属性,即 ActionDispatch::Http::UploadedFile。当调用 @user.save 时,图像不会被处理或保存。
线索、教程或解决方案将不胜感激!
I've been looking for a way to set up Ruby on Rails 3.1 with Paperclip and jQuery fileupload. Looking at a tutorial at jQuery fileupload page I got the system set up but I can't find a way to make paperclip process the uploaded file.
Here's what I have (in short):
# model
has_attached_file :photo ...
# view
= form_for @user, :html => {:multipart => true} do |f|
f.file_field :photo, :multiple => true
f.submit
# controller
def create
@user = User.new params[:user]
if @user.save
render :json => [...]
end
If I inspect submitted data I get all user properties in params[:user] and params[:user][:photo] which is ActionDispatch::Http::UploadedFile. When @user.save is called the image doesn't get processed or saved.
A clue, tutorial or a solution would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在解决这个问题一段时间后,我发现问题出现在
:multipart =>; 时。 true
被添加到f.file_field
,因为表单字段名称从user[photo]
更改为user[photo][]
>。使用单独的页面附加照片
我希望有一个单独的页面用于将多个文件上传到记录(以及另一个用于编辑用户属性的页面)。在我看来,这只是一个临时解决方案,但它确实有效。而不是
f.form_field :photo, :multipart => true
我使用了form_field_tag 'user[photo]', :multiple =>; true
在视图中。我的代码如下所示:
如果其他人知道更好的方法(正确的方法),请告诉我们!
使用 fields_for
根据应用的结构,您可能会考虑使用
fields_for
。在这种情况下,您必须:
accepts_nested_attributes_for :photos
添加到您的(用户)模型photos_attribues=(attributes)
添加到您的(用户)模型并处理记录 在用户的新方法中创建3.times { @user.photos.build }
示例:
免责声明:上面的代码已被简化/重写使其更容易理解。我可能在删除不需要的东西时犯了错误。再说一遍 - 我不太确定这是解决这个问题的正确方法。非常欢迎提出建议和改进!
After struggling with this problem for a while I discovered the problem appeared when
:multipart => true
is added to thef.file_field
since the form field name changes fromuser[photo]
touser[photo][]
.Using separate page to attach photos
I want to have a separate page for uploading multiple files to a record (and another one for editing User's properties). This looks to me as a temporary solution but it works. Instead of
f.form_field :photo, :multipart => true
I usedform_field_tag 'user[photo]', :multiple => true
in the view.My code looks like this:
If anyone else knows a better way (the right way) of doing this, please let us know!
Using fields_for
Depending on the structure of your app you might consider using
fields_for
.In this case you'll have to:
accepts_nested_attributes_for :photos
to your (User) modelphotos_attribues=(attributes)
to your (User) model and handle record creation there3.times { @user.photos.build }
in User's new methodExample:
Disclaimer: The code above was simplified/rewritten to make it easier to understand. I might've made mistakes while erasing unneeded stuff. And again - I'm not really sure this is the right way of solving this problem. Suggestions and improvements are more than welcome!
如果您在
file_field
上显式设置名称:Rails 将生成
user[photo]
而不是user[photo][]
。If you explicitly set the name on the
file_field
:Rails will generate
user[photo]
instead ofuser[photo][]
.