2013-10-31 57 views
13

我一直在敲我的頭撞在牆上試圖讓Carrierwave,軌道4,5和多上傳所有一起工作。我可以像在這個和其他許多項目中一樣使用單個文件上傳。Carrierwave,軌道4,5和多上傳

這不是一個嵌套的情況 - 只是單純的上傳到所謂的轉錄單一的模式,並希望創造上傳的每個文檔的記錄。

我似乎無法找到申報用於carrierwave「文件」字段中輸入正確的方式安裝

mount_uploader :document, DocumentUploader 

爲強參數來識別一個數組。

我已經試過白名單:whitelisted[:document] = params[:transcription]['document']

聲明「文件」作爲一個數組:

params.require(:transcription).permit(..... ,:document => []) 

params.require(:transcription).permit(..... , { document: [] }) 

這一切似乎更像我聲明瞭一個嵌套模式陣列,但我真的想軌道4的強參數簡單地看到由是,file_field創建的「文件」陣列:多個=>真

即從日誌:form-data; name=\"transcription[document][]

有沒有人成功地完成了在軌道4,5多個上傳具有較強的參數?如果是的話,你願意分享嗎?

謝謝...

乾杯,

比爾

回答

1

CarrierWave不支持多個上傳。它旨在將單個文件與單個字段相關聯。

如果需要多個上傳,你需要或者多個字段(每一個CarrierWave上傳),或多個對象各自具有一個CarrierWave上傳領域。

multiple屬性也是不支持的,所以如果你使用它,它是完全由你來獲取有關分配適當的參數。

+1

這是不正確的。它支持單個字段內的多個上傳。看到我的答案。 carrierwave提供了更多。 – SSR

+1

需要說明的是:您的解決方案使用多個對象(post_attachments),每個對象都有一個上傳器,並演示爲了使用「多個」屬性正確分配參數所需的手動工作。你已經演示瞭如何去做OP所需要的,但是它並沒有把上面的任何部分弄錯。 – Taavo

+0

時間不斷前進。地球旋轉並逐漸變化,CarrierWave現在支持本地多文件上傳。然而,CarrierWave的做法是一個醜陋的黑客攻擊(使用數組引用所有文件),我會避免使用它。改爲使用SSR的解決方案。 –

0

我想創建一個名爲與同時安裝在現場的文檔模型

class Documents < ActiveRecord::Base 
    belongs_to :transcription 

    mount_uploader :doc, DocumentUploader 
end 

class Transcriptions < ActiveRecord::Base 
    has_many :documents 
end 

我仍然有用戶在我的控制器下面一行:

params.require(:transcription).permit(..... , { document: [] }) 
36

這是解決方案從頭上傳在導軌4使用carrierwave多個圖像

只需按照以下步驟操作即可。

rails new multiple_image_upload_carrierwave 

在寶石文件

gem 'carrierwave' 
bundle install 
rails generate uploader Avatar 

創建後支架

rails g scaffold post title:string 

創建post_attachment支架

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

def update 
    respond_to do |format| 
    if @post.update(post_params) 
     params[:post_attachments]['avatar'].each do |a| 
     @post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id) 
     end 
    end 
    end 

    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to @post } 
     format.json { head :no_content } 
    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 %> 

    <% if params[:controller] == "post" && params[:action] == "edit" %> 
    <% @post.post_attachments.each do |p| %> 
     <%= image_tag p.avatar, :size => "150x150" %> 
    <% end %> 
    <% 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, :size => "150x150" %> 
    <%= link_to "Destroy", p, method: :delete %> 
<% end %> 

<%= link_to 'Edit', edit_post_path(@post) %> | 
<%= link_to 'Back', posts_path %> 

在導軌3不需要定義強的參數和作爲您可以在模型中定義attribute_accessible,並在post模型中定義accept_nested_attribute,因爲在rails 4中不推薦使用可訪問屬性。

+0

偉大的帖子@SSR,只有多次上傳我設法得到工作。不要以爲你能稍微擴展它?在當前形式下,父級模型不會刪除文件。 (或不要爲我),我想知道如何去編輯帖子和它的附件在一起。 – Darkstarone

+0

基本上我想知道post_controller.rb看起來像正確編輯和刪除附件? – Darkstarone

+0

謝謝,它幫助我,我的意思是這個主意! – Rubyrider