6

我试图支持uploadify,但它给了我这个错误。基本上我有一个命名为. 对于单次上传,它适用于carrierwave,但对于多个文件则不然。carrierwavemultiple file uploadGET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739modeluser

我已经按照教程进行操作。在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>

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

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

   mount_uploader :image, ImageUploader 
 end

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

4

1 回答 1

1

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

rails new multiple_image_upload_carrierwave

在宝石文件中

gem 'carrierwave'

然后运行以下

bundle install
rails generate uploader Avatar 

创建后脚手架

rails generate scaffold post title:string

创建 post_attachment 脚手架

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

更新表单以编辑附件视图/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 中不推荐使用属性可访问性。

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

于 2015-02-07T18:15:11.100 回答