Ruby on rails 多个图像上传到rails-更新多个模型
我正在尝试使用“jquery fileupload Rails”和“carrierwave”gems在我的Rails应用程序中实现多个文件上传功能 该应用程序基本上有多个步骤的项目,每个步骤可以有多个图像 我试图上传图片,而在一个更新特定步骤的形式。当我试图上传图像时,似乎什么也没发生。如果你能帮我解释一下,我将永远感激你!提前谢谢 routes.rb:Ruby on rails 多个图像上传到rails-更新多个模型,ruby-on-rails,carrierwave,jquery-file-upload,Ruby On Rails,Carrierwave,Jquery File Upload,我正在尝试使用“jquery fileupload Rails”和“carrierwave”gems在我的Rails应用程序中实现多个文件上传功能 该应用程序基本上有多个步骤的项目,每个步骤可以有多个图像 我试图上传图片,而在一个更新特定步骤的形式。当我试图上传图像时,似乎什么也没发生。如果你能帮我解释一下,我将永远感激你!提前谢谢 routes.rb: Build::Application.routes.draw do ActiveAdmin.routes(self) devise
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
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
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
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
<div class="row span12">
<div class="row">
<%= render 'step_form' %>
</div>
</div>
<%= 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 %>
// 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
步骤。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
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
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
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
<div class="row span12">
<div class="row">
<%= render 'step_form' %>
</div>
</div>
<%= 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 %>
// 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
你可以发布你的
application.js
文件吗?我编辑了我的原始帖子,以包含application.js文件的内容。你是否创建了与多文件上传相关的javascript?关于这个话题有一个很好的解释。这将对你的情况非常有帮助。我一直在学习该教程以及最初的carrierwave教程(用于单文件上传)。不幸的是,它仍然不起作用。我认为这可能与我设置模型的方式有关,因为它们不像他的示例中那样嵌套。