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

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

问题描述

我一直在寻找一种方法来设置Ruby on Rails 3.1,并使用 Paperclip jQuery文件上传。查看 jQuery文件上传页面上的教程我设置了系统但我找不到一个方法来使回形针处理上传的文件。

这是我有(简而言之):

 #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被调用时,图像没有得到处理或保存。



一个线索,教程或解决方案将不胜感激!

解决方案

经过一段时间的努力,我发现问题出现在:multipart =>由于表单字段名称从 user [photo] 更改,所以添加到 f.file_field c>到用户[照片] [] 使用单独页面附加照片



我想要一个单独的页面上传多个文件到一个记录(和另一个编辑用户的属性)。这看起来像一个临时的解决方案,但它的工作。而不是 f.form_field:photo,:multipart => true 我用 form_field_tag'user [photo]',:multiple =>真正在视图中。

我的代码如下所示:

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

def to_fileupload_json
{
name=> photo_file_name,
size=> photo_file_size,
...
}
结束

## 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_tagjquery.fileupload.js
#需要插件所需的所有其他JS文件(jQuery,jQuery UI,...)
= stylesheet_link_tagjquery.fileupload-ui.css
= render:partial => jquery_file_templates#部分用于jQuery模板的响应

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


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

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

使用fields_for



根据应用程序的结构, code> fields_for



在这种情况下,您必须:

添加一个方法<$ c

  • 为您的(用户)模型添加 accepts_nested_attributes_for:photos $ c> photos_attribues =(属性)添加到您的(用户)模型并处理记录创建
  • 创建照片记录 3.times用户新方法中的{@ user.photos.build}


    le:

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

    上面的代码被简化/重写,使其更容易理解。消除不必要的东西,我可能犯了错误。再次 - 我不确定这是解决这个问题的正确方法。建议和改进比欢迎!


    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!

    解决方案

    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][].

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

    My code looks like this:

    ## 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!

    Using fields_for

    Depending on the structure of your app you might consider using fields_for.

    In this case you'll have to:

    • 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

    Example:

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

    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!

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

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