Rails 3 + JQuery-File-Upload +嵌套模型

use*_*023 12 file-upload ruby-on-rails nested-attributes dragonfly-gem

我一直在寻找一些例子,但是很简单:

我正在尝试在我正在处理的项目上实现JQuery-File-Upload,但是我很想知道如何使用嵌套属性.

快速概述:

2型号:

Comment [has_many :attachments]
Attachment [belongs_to :comment]
Run Code Online (Sandbox Code Playgroud)

评论accepted_nested_attributes_for :attachments.另外 - 我正在使用Dragonfly.

我已经回顾了JQuery-File-Upload网站上的Rails 3指南,但是他们认为它是一个单一的模型,所以它都是围绕一个表单构建的.有没有人有任何他们的实现的例子,或者是否有一个我还没有偶然发现的现有教程?

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

Kyl*_*son 6

我只是想在这里和Stone一起回答我的问题.我花了近两天时间才开始工作(Stone是对的,它是PITA!),所以希望我的解决方案可以帮助别人.我做的只是与Stone不同的触摸.

我的应用程序Features(漫画,拼图,文本列等)和FeatureAssets(个别漫画面板/颜色版本,特定填字游戏的问答文件等).因为FeatureAssets它只与一个相关Feature,所以我嵌套了模型(正如你在我的上传表格中看到的那样).

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

希望我能清楚地翻译这个.我宁愿提供的信息太多而不够.当你解释别人的代码时,一点额外的背景永远不会受到伤害.

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

并不是说它可能有多大帮助,但我的feature_asset_uploader.rb在下面.这很糟糕.

class FeatureAssetContentUploader < CarrierWave::Uploader::Base

  storage :file

end
Run Code Online (Sandbox Code Playgroud)

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

<%= 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>
Run Code Online (Sandbox Code Playgroud)

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

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

凯尔


Sto*_*one 1

我在 Carrierwave 上运行了类似的设置。这就是我所拥有的。我使用图像作为项目的嵌套资源。

项目.rb:

has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)

图像.rb:

 include Rails.application.routes.url_helpers
  mount_uploader :image, ImageUploader

  belongs_to :project

  #one convenient method to pass jq_upload the necessary information
  def to_jq_upload
  {
    "name" => read_attribute(:image),
    "size" => image.size,
    "url" => image.url,
    "thumbnail_url" => image.thumb.url,
    "delete_url" => image_path(:id => id),
    "delete_type" => "DELETE" 
   }
  end
Run Code Online (Sandbox Code Playgroud)

Images_controller.rb:

 def create
    @image = Image.new(params[:image])
    @image.project_id = params[:project_id]
    @project = Project.find(params[:project_id])
    @image.position = @project.images.count + 1
    if @image.save
      render :json => [ @image.to_jq_upload ].to_json
    else
      render :json => [ @image.to_jq_upload.merge({ :error => "custom_failure" }) ].to_json
    end
  end
Run Code Online (Sandbox Code Playgroud)

请注意,这是 *!@^%! 开始工作。

更新:projects/_form.html.erb

<div id="fileupload" class="image_add">
    <%= form_for Image.new, :html => {:multipart => true} do |f| %>
        <div class="fileupload-buttonbar">
            <label class="fileinput-button">
                <span>Add files...</span>
                <%= hidden_field_tag :project_id, @project.id %>
                <%= f.file_field :image %>
            </label>
            <button type="submit" class="start">Start Upload</button>
            <button type="reset" class="cancel">Cancel Upload</button>
            <button type="button" class="delete">Delete Files</button>
        </div>
    <% end %>
    <div class="fileupload-content">
        <div class="dropzone-container">
            <div class="dropzone">Drop Image Files Here</div>
        </div>
        <table class="files"></table>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)