使用carrierwave和uploadify时上传错误

Ahm*_*mza 6 ruby-on-rails uploadify mongoid carrierwave

我试图为uploadifycarrierwave支持multiple file upload,但它给我这个错误GET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739.基本上我有一个model名为user.对于单个上传,它可以与carrierwave一起正常工作,但对于多个文件则不然.

我已经按照教程.在users/_form.rb

<p>
<%= f.label "Upload Images"%>
<%= f.file_field :image, :multiple => true %>
</p>

<script type= "text/javascript">
$(document).ready(function() {
 <% key = Rails.application.config.session_options[:key] %>
  var uploadify_script_data = {};
  var csrf_param = $('meta[name=csrf-param]').attr('content');
  var csrf_token = $('meta[name=csrf-token]').attr('content');
  uploadify_script_data[csrf_param] = encodeURI(encodeURIComponent(csrf_token));
  uploadify_script_data['<%= key %>'] = '<%= cookies[key] %>';

  $('#user_image').uploadify({
  uploader        : '<%= asset_path("uploadify.swf")%>',
  script          : '/images',
  cancelImg       : '<%= asset_path("uploadify-cancel.png")%>',
  auto            : true,
  multi           : true,
  removeCompleted     : true,
  scriptData      : uploadify_script_data,
  onComplete      : function(event, ID, fileObj, doc, data) {
  }
  });  
 });
</script>
Run Code Online (Sandbox Code Playgroud)

我使用mongoid所以模型是这样的

class User
 include Mongoid::Document
 field :name, type: String
 field :description, type: String
 field :image, type: String

   mount_uploader :image, ImageUploader 
 end
Run Code Online (Sandbox Code Playgroud)

我没有得到什么是错误.请帮帮我.

And*_*rie 1

这是使用 Carrierwave 上传多个图像的完整解决方案:只需按照以下步骤操作即可。

rails new multiple_image_upload_carrierwave
Run Code Online (Sandbox Code Playgroud)

在 gem 文件中

gem 'carrierwave'
Run Code Online (Sandbox Code Playgroud)

然后运行以下命令

bundle install
rails generate uploader Avatar 
Run Code Online (Sandbox Code Playgroud)

创建柱脚手架

rails generate scaffold post title:string
Run Code Online (Sandbox Code Playgroud)

创建 post_attachment 脚手架

rails generate scaffold post_attachment post_id:integer avatar:string
Run Code Online (Sandbox Code Playgroud)

然后运行

rake db:migrate
Run Code Online (Sandbox Code Playgroud)

在post.rb中

class Post < ActiveRecord::Base
   has_many :post_attachments
   accepts_nested_attributes_for :post_attachments
end
Run Code Online (Sandbox Code Playgroud)

在 post_attachment.rb 中

class PostAttachment < ActiveRecord::Base
   mount_uploader :avatar, AvatarUploader
   belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)

在 post_controller.rb 中

def show
   @post_attachments = @post.post_attachments.all
end

def new
   @post = Post.new
   @post_attachment = @post.post_attachments.build
end

def create
   @post = Post.new(post_params)

   respond_to do |format|
     if @post.save
       params[:post_attachments]['avatar'].each do |a|
         @post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id)
       end
       format.html { redirect_to @post, notice: 'Post was successfully created.' }
     else
       format.html { render action: 'new' }
     end
   end
 end

 private
   def post_params
      params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
   end
Run Code Online (Sandbox Code Playgroud)

在views/posts/_form.html.erb中

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
   <div class="field">
     <%= f.label :title %><br>
     <%= f.text_field :title %>
   </div>

   <%= f.fields_for :post_attachments do |p| %>
     <div class="field">
       <%= p.label :avatar %><br>
       <%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
     </div>
   <% end %>

   <div class="actions">
     <%= f.submit %>
   </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

编辑任何帖子的附件和附件列表。在views/posts/show.html.erb中

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<% @post_attachments.each do |p| %>
  <%= image_tag p.avatar_url %>
  <%= link_to "Edit Attachment", edit_post_attachment_path(p) %>
<% end %>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
Run Code Online (Sandbox Code Playgroud)

更新表单以编辑附件views/post_attachments/_form.html.erb

<%= image_tag @post_attachment.avatar %>
<%= form_for(@post_attachment) do |f| %>
  <div class="field">
    <%= f.label :avatar %><br>
    <%= f.file_field :avatar %>
  </div>
  <div class="actions">
     <%= f.submit %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

修改post_attachment_controller.rb中的update方法

def update
  respond_to do |format|
    if @post_attachment.update(post_attachment_params)
      format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' }
    end 
  end
end
Run Code Online (Sandbox Code Playgroud)

在 Rails 3 中,无需定义强参数,因为您可以在模型中定义 attribute_accessible ,并在发布模型时定义 Accept_nested_attribute ,因为在 Rails 4 中不推荐使用属性accessible。

对于编辑附件,我们无法一次修改所有附件。所以我们会一一替换附件,或者您也可以根据您的规则进行修改,这里我只是向您展示如何更新任何附件。