我正在尝试使用“jquery-fileupload-rails”和“carrierwave”gem 在我的 Rails 应用程序中实现多个文件上传功能。
该应用程序基本上具有具有多个步骤的项目,并且每个步骤可以有多个图像。
我正在尝试以更新特定步骤的形式上传图像。当我尝试上传图片时,似乎什么都没有发生。如果您能帮助阐明一些问题,我将永远感激不尽!提前致谢。
路线.rb:
Build::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
resources :projects do
resources :steps
match "steps/:id" => "steps#number", :as => :number
end
resources :images
root :to => 'projects#index'
end
步骤.rb:
class Step < ActiveRecord::Base
extend FriendlyId
friendly_id :number
attr_accessible :description, :name, :number, :project_id, :images_attributes
belongs_to :project
has_many :images
accepts_nested_attributes_for :images, :allow_destroy => :true
end
图像.rb:
class Image < ActiveRecord::Base
attr_accessible :project_id, :step_id, :file, :caption, :crop_x, :crop_y, :crop_w, :crop_h
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
belongs_to :step
belongs_to :project
mount_uploader :file, FileUploader
end
文件上传器.rb:
class FileUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"/assets/"
# "#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
version :thumb do
process resize_to_fill: [420,315]
end
end
意见/步骤/edit.html.erb:
<div class="row span12">
<div class="row">
<%= render 'step_form' %>
</div>
</div>
views/steps/_step_form.html.erb(这通过部分处理更新图像模型,如下所示):
<%= semantic_form_for [@project, @step], :html => {:multipart => true} do |f| %>
<%= f.inputs do%>
....
<div class = "imageGalleryButtons">
<span class="btn btn-mini fileinput-button">
<i class = "icon-plus"></i>
Add Photo
<%= render :partial => 'images/upload_form', :locals => {:form => f} %>
</span>
<%= link_to 'Reorder Photos', '#', :class=> "btn btn-mini" %>
<!--<button class="btn btn-large">Reorder Photos</button>-->
</div>
<div class = "clear"></div>
</div>
<% end %>
<% end %>
视图/图像/_upload_form.html.erb
<%= form.fields_for :images, :html => {:multipart => true} do |image_form| %>
<%= image_form.file_field :file, :multiple=>true%>
<% end %>
应用程序.js:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-fileupload/basic
//= require twitter/bootstrap
//= require_tree
//= require js-routes