2013-02-24 84 views
6

我正在嘗試uploadifycarrierwave以支持multiple file upload,但它給了我這個錯誤GET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739。 基本上我有一個model命名爲user。對於單一上傳,它與Carrierwave正常工作,但對於多個文件,它不是。使用carrierwave和uploadify時上傳錯誤

我跟着this教程。 在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 

我不是得到什麼是錯誤。請幫助我。

+0

您只發布了GET請求,而不是實際的錯誤。請發佈錯誤。 – Jesper 2013-02-24 12:53:28

+0

這是我進入js控制檯的錯誤。其實,瀏覽文件按鈕不起作用。 – 2013-02-24 20:18:32

+0

請理解'GET http:// localhost:3000/users/uploadify.swf?preventwfcaching = 1361694618739'並不是錯誤,它只是一個聲明。請給我們實際的錯誤。 – Jesper 2013-02-24 20:37:55

回答

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 

在意見/職位/ _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 %> 

要編輯任何職位的附件和附件列表。在視圖/帖/ 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中,不需要定義強參數,因爲您可以在模型中定義attribute_accessible,並在post模型中定義accept_nested_attribute,因爲可訪問屬性已棄用導軌4。

對於編輯附件,我們無法一次修改所有附件。所以我們會逐一替換附件,或者您可以按照您的規則進行修改,這裏我只是告訴您如何更新任何附件。

+1

最近,但感謝您的回覆!!! – 2015-02-08 07:46:05

+0

此答案解決了您的問題,還是需要更多信息? – 2015-02-08 16:51:23

+0

忘記了標記它 – 2015-02-08 19:17:19