Rails 3 + JQuery-File-Upload +嵌套模型
我一直在寻找一些例子,但是很简单:
我正在尝试在我正在处理的项目上实现JQuery-File-Upload,但是我很想知道如何使用嵌套属性。
快速概述:
2型号:
Comment [has_many :attachments] Attachment [belongs_to :comment]
评论accepted_nested_attributes_for :attachments
。 另外 – 我正在使用Dragonfly。
我已经回顾了JQuery-File-Upload网站上的Rails 3指南,但是他们认为它是一个单一的模型,所以它都是围绕一个表单构建的。 有没有人有任何他们的实现的例子,还是有一个我还没有偶然发现的现有教程?
我确定有人有类似的问题…是JQuery-File-Upload到适当的工具还是我应该看看别的什么?
我只是想在这里和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
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
并不是说它可能有多大帮助,但我的feature_asset_uploader.rb在下面。 这很糟糕。
class FeatureAssetContentUploader < CarrierWave::Uploader::Base storage :file end
function_form.html.erb(类似Stone的,但不完全)
<%= form_for [@feature, @feature_asset], :html => { :multipart => true } do |f| %>
它没有error handling或它应该具有的任何细节,但这是它的准系统版本。
希望这有助于那里的人。 如果您有任何疑问,请随时问我!
凯尔
我有一个与Carrierwave一样的设置。 这就是我所拥有的。 我正在使用Images作为Projects的嵌套资源。
Project.rb:
has_many :images, :dependent => :destroy accepts_nested_attributes_for :images, :allow_destroy => true
Image.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
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
请注意,这是一个*!@ ^%! 开始工作
更新:projects / _form.html.erb
<%= form_for Image.new, :html => {:multipart => true} do |f| %> Drop Image Files Here
<% end %>
我已经解决了这个问题并制作了一个演示应用程序来展示如何做到这一点。
总之,我有两个模型:项目和上传。
item.rb的:
has_many :uploads accepts_nested_attributes_for :uploads, :allow_destroy => true
upload.rb:
belongs_to :item has_attached_file :upload, :styles => { :large => "800x800", :medium => "400x400>", :small => "200x200>" }
我将uploads_attributes
添加到项目控制器。
现在,您可以将jquery-file-upload表单添加到视图中,但存在一个问题:它会在单独的请求中发送每张照片。 所以有我的jquery-file-upload初始化程序,它在一个请求中上传所有照片(创建项目模型),然后重定向到你的应用程序的根目录(你需要使用项目表单):