Rails 3 + JQuery 文件上传 + 嵌套模型 [英] Rails 3 + JQuery-File-Upload + Nested Model

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

问题描述

我一直在寻找一些示例,但没有找到:

我正在尝试在我正在处理的项目上实现 JQuery-File-Upload,但我对如何让它与嵌套属性一起工作感到迷茫.

快速概览:

2 个模型:

评论 [has_many :attachments]附件 [belongs_to :comment]

评论接受_nested_attributes_for :attachments.另外 - 我正在使用蜻蜓.

我在 JQuery-File-Upload 站点上查看了 Rails 3 指南,但他们认为它是一个单一的模型,所以它都是围绕一个表单构建的.有没有人有任何实现的示例,或者是否有我尚未偶然发现的现有教程?

我确定有人遇到过类似的问题...是 JQuery-File-Upload 到适当的工具还是我应该看看其他东西?

解决方案

我只是想把我的答案和斯通的答案放在一起.我花了将近两天的时间来让它工作(斯通是对的,这是一个 PITA!),所以希望我的解决方案能帮助别人.我的做法与 Stone 略有不同.

我的应用程序具有 Features(漫画、拼图、文本栏等)和 FeatureAssets(单个漫画面板/颜色版本、问题和答案文件)特定的填字游戏等).由于 FeatureAssets 仅与一个 Feature 相关,因此我嵌套了模型(如您在我的上传表单中所见).

对我来说最大的问题是意识到我发送到服务器的 params[:feature_asset] 实际上是我上传者的 file 对象数组,而不是我习惯使用的那个.在对每个文件进行迭代并从中创建一个 FeatureAsset 之后,它就像一个魅力!

希望我能把这个翻译清楚.我宁愿提供过多的信息也不愿提供足够的信息.当你解释别人的代码时,一点额外的上下文永远不会受到伤害.

功能.rb

class Feature 真的验证 :name, :presence =>真的验证 :user_id, :presence =>真的mount_uploader :image, FeatureImageUploader结尾

feature_asset.rb

belongs_to :user归属地:特征attr_accessible :user_id, :feature_id, :file, :file_cache验证 :user_id, :presence =>真的验证 :feature_id, :presence =>真的验证 :file, :presence =>真的mount_uploader :文件,FeatureAssetContentUploader# 抓取有用的文件属性 &将它们作为 JSON 发送到 jQuery 文件上传器def to_jq_upload{文件"=>文件,文件名"=>'asdf',网址"=>文件.url,delete_url" =>ID,删除类型" =>删除"}结尾

feature_assets_controller.rb

 def 创建@feature = Feature.find(params[:feature_id])params[:feature_asset]['file'].each do |f|@feature_asset = FeatureAsset.create!(:file => f, :feature_id => @feature.id, :user_id => current_user.id)结尾重定向到@feature结尾

并不是说它可能有多大帮助,但我的 feature_asset_uploader.rb 在下面.它相当精简.

class FeatureAssetContentUploader 

功能 _form.html.erb(类似于 Stone 的,但不完全相同)

<%= form_for [@feature, @feature_asset], :html =>{ :multipart =>真 } 做 |f|%><div class="row" id="fileupload"><div class="fileupload-buttonbar"><div class="progressbar fileupload-progressbar nofade"><div style="width:0%;"></div></div><span class="btn btn-primary fileinput-button"><i class="icon-plus"></i><span><%= t('feature_assets.add_files') %>...</span><%= hidden_​​field_tag :feature_id, @feature.id %><%= hidden_​​field_tag :user_id, current_user.id %><%= f.file_field :file, :multiple =>真%></span><button type="submit" class="btn btn-success">开始上传</button><button type="reset" class="btn btn-warning">取消上传</button><button type="button" class="btn btn-danger">删除文件</button>

它没有错误处理或任何它应该有的细节,但这是它的准系统版本.

希望能帮助到那里的人.如果您有任何问题,请随时问我!

凯尔

I've been searching for some examples, but have come up short:

I'm trying to implement JQuery-File-Upload on a project I'm working on, but am getting lost as to how to get it to work with nested attributes.

Quick overview:

2 Models:

Comment [has_many :attachments]
Attachment [belongs_to :comment]

Comment accepts_nested_attributes_for :attachments. Also - I'm using Dragonfly.

I've reviewed the Rails 3 guides on the JQuery-File-Upload site, but they assume it's a singular model, so it's all built around a form. Does anyone have any examples of their implementation or is there an existing tutorial that I haven't yet stumbled across?

I'm sure someone has had a similar issue... is JQuery-File-Upload to appropriate tool or should I look at something else?

解决方案

I just wanted to throw my answer in here as well as Stone's. I spent nearly two solid days getting this to work (Stone was right, it was a PITA!), so hopefully my solution will help someone. I did it just a touch different than Stone.

My app has Features (a comic, puzzle, text-column, etc) and FeatureAssets (individual comic panels/color versions, question & answer files for a specific crossword, etc). Since FeatureAssets are solely related to one Feature, I nested the models (as you'll see in my upload form).

The biggest problem for me was realizing that my params[:feature_asset] that was being sent to the server was actually an array of my uploader'd file objects, instead of just the one I was used to working with. After a bit of fiddling with iterating through each file and creating a FeatureAsset from it, it worked like a charm!

Hopefully I'll translate this clearly. I'd rather provide a bit too much information than not enough. A little extra context never hurts when you're interpreting someone else's code.

feature.rb

class Feature < ActiveRecord::Base
  belongs_to :user
  has_many :feature_assets

  attr_accessible :name, :description, :user_id, :image

  accepts_nested_attributes_for :feature_assets, :allow_destroy => true

  validates :name,    :presence => true
  validates :user_id, :presence => true

  mount_uploader :image, FeatureImageUploader
end

feature_asset.rb

  belongs_to :user
  belongs_to :feature

  attr_accessible :user_id, :feature_id, :file, :file_cache

  validates :user_id,     :presence => true
  validates :feature_id,  :presence => true
  validates :file,        :presence => true

  mount_uploader :file, FeatureAssetContentUploader

  # grabs useful file attributes & sends them as JSON to the jQuery file uploader
  def to_jq_upload
    {
      "file" => file,
      "file_name" => 'asdf',
      "url" => file.url,
      "delete_url" => id,
      "delete_type" => "DELETE"
    }
  end

feature_assets_controller.rb

  def create
    @feature = Feature.find(params[:feature_id])

    params[:feature_asset]['file'].each do |f|
      @feature_asset = FeatureAsset.create!(:file => f, :feature_id => @feature.id, :user_id => current_user.id)
    end

    redirect_to @feature
  end

And not that it probably helps that much, but my feature_asset_uploader.rb is below. It's pretty stripped down.

class FeatureAssetContentUploader < CarrierWave::Uploader::Base

  storage :file

end

features _form.html.erb (similar to Stone's, but not quite)

<%= form_for [@feature, @feature_asset], :html => { :multipart => true  } do |f| %>
  <div class="row" id="fileupload">
    <div class=" fileupload-buttonbar">
      <div class="progressbar fileupload-progressbar nofade"><div style="width:0%;"></div></div>
      <span class="btn btn-primary fileinput-button">
        <i class="icon-plus"></i>
        <span><%= t('feature_assets.add_files') %>...</span>
        <%= hidden_field_tag :feature_id, @feature.id %>
        <%= hidden_field_tag :user_id, current_user.id %>
        <%= f.file_field :file, :multiple => true %>
      </span>
      <button type="submit" class="btn btn-success">Start Upload</button>
      <button type="reset" class="btn btn-warning">Cancel Upload</button>
      <button type="button" class="btn btn-danger">Delete Files</button>
    </div>
  </div>

It doesn't have error handling or any of the niceties it should have, but that's the barebones version of it.

Hopefully that helps someone out there. Feel free to ask me if you have any questions!

Kyle

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

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