使用carrierwave和uploadify时上传错误

时间:2013-02-24 08:52:35

标签: ruby-on-rails mongoid uploadify carrierwave

我正在尝试使用uploadify carrierwave来支持multiple file upload,但它正在向我发出此错误GET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739。 基本上我有一个名为model的{​​{1}}。对于单个上传,它适用于carrierwave,但对于多个文件,它不是。

我遵循了this教程。 在user

users/_form.rb

我使用mongoid所以模型就像这样

<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>

我没有得到错误。请帮帮我。

1 个答案:

答案 0 :(得分:1)

这是使用Carrierwave进行多张图片上传的完整解决方案: 要执行这些步骤。

rails new multiple_image_upload_carrierwave

在宝石文件中

gem 'carrierwave'

然后运行以下

bundle install
rails generate uploader Avatar 

创建帖子支架

rails generate scaffold post title:string

创建post_attachment scaffold

rails generate scaffold post_attachment post_id:integer avatar:string

然后运行

rake db:migrate

在post.rb

class Post < ActiveRecord::Base
   has_many :post_attachments
   accepts_nested_attributes_for :post_attachments
end

在post_attachment.rb

class PostAttachment < ActiveRecord::Base
   mount_uploader :avatar, AvatarUploader
   belongs_to :post
end

在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

在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 %>

编辑任何帖子的附件和附件列表。在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 %>

更新表单以编辑附件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 %>

修改post_attachment_controller.rb中的更新方法

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

在rails 3中,无需定义强参数,因为您可以在模型和accept_nested_attribute中定义attribute_accessible以发布模型,因为在rails 4中不推荐使用可访问属性。

要编辑附件,我们无法一次修改所有附件。所以我们将逐个替换附件,或者您可以根据您的规则进行修改,我只是告诉您如何更新任何附件。