Rails 3.1 + 回形针 + jQuery 文件上传 [英] Rails 3.1 + Paperclip + jQuery fileupload

查看:17
本文介绍了Rails 3.1 + 回形针 + jQuery 文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来使用 PaperclipjQuery 文件上传.查看 jQuery fileupload 页面上的教程,我设置了系统但我找不到让回形针处理上传文件的方法.

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

如果我检查提交的数据,我会在 params[:user] 和 params[:user][:photo] 中获得所有用户属性,即 ActionDispatch::Http::UploadedFile.当@user.save 被调用时,图像不会被处理或保存.

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!

推荐答案

在这个问题苦苦挣扎了一段时间后,我发现问题出现在 :multipart =>true 添加到 f.file_field 因为表单字段名称从 user[photo] 更改为 user[photo][].

After struggling with this problem for a while I discovered the problem appeared when :multipart => true is added to the f.file_field since the form field name changes from user[photo] to user[photo][].

使用单独的页面附加照片

我希望有一个单独的页面用于将多个文件上传到一条记录(以及另一个用于编辑用户属性的页面).这在我看来是一个临时解决方案,但它确实有效.而不是 f.form_field :photo, :multipart =>是的 我使用了 form_field_tag 'user[photo]', :multiple =>true 在视图中.

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 used form_field_tag 'user[photo]', :multiple => true in the view.

我的代码如下所示:

## app/model/user.rb
has_attached_file :photo

def to_fileupload_json
  {
    "name" => photo_file_name,
    "size" => photo_file_size,
    ...
  }
end

## app/views/photos/new.html.haml
= form_for @user, :url => users_path, :html => { :multipart => true } do |f|
  #fileupload
    = file_field_tag 'user[photo]', :multiple => true
    = f.submit

= javascript_include_tag "jquery.fileupload.js"
# require all other JS files needed for the plugin (jQuery, jQuery UI, ...)
= stylesheet_link_tag "jquery.fileupload-ui.css"
= render :partial => "jquery_file_templates" # partial with jQuery templates for responses 

:javascript
  $(function () {
      uploader = $('#fileupload').fileupload()
  }


## app/controllers/users_controller.rb
def create
  @user = User.create params[:user]
end

如果其他人知道这样做的更好方法(正确方法),请告诉我们!

If anyone else knows a better way (the right way) of doing this, please let us know!

使用 fields_for

根据您应用的结构,您可能会考虑使用 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 添加到您的(用户)模型中
  • 将方法 photos_attribues=(attributes) 添加到您的(用户)模型并在那里处理记录创建
  • 在用户的新方法中3.times { @user.photos.build } 建立照片记录
  • add accepts_nested_attributes_for :photos to your (User) model
  • add a method photos_attribues=(attributes) to your (User) model and handle record creation there
  • build record for photos 3.times { @user.photos.build } in User's new method

例子:

def photos_attribues=(attributes)
  attributes.each do |key, value|
    photo = Photo.create :photo => value, ...
  end
end

免责声明:上面的代码经过简化/重写以使其更易于理解.我可能在删除不需要的东西时犯了错误.再说一次 - 我不确定这是解决这个问题的正确方法.欢迎提出建议和改进!

这篇关于Rails 3.1 + 回形针 + jQuery 文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆