2016-03-08 38 views
0

我有模型項目和在模型中有字段文件(文件)。我正在使用寶石蜻蜓。編輯項目時,如何製作當前文件列表?此時在編輯頁面上的文件列表顯示:「該文件未被選中」。我下一步:寶石蜻蜓。如何在編輯視圖中創建當前文件的列表?

projects_controller:

def edit 

    end 

    def update 
     if @project.update(project_params) 
     redirect_to @project 
     else 
     render :edit 
     end 
    end 

    private 

    def set_project 
     @project = Project.find(params[:id]) 
    end 

    def set_payment_types 
     @payment_types = PaymentOption.all 
    end 

    def project_params 
     params.require(:project).permit(:title, :description, :price, :location, 
     :anonymity, :price_category, :category_id, :skill_list, documents_attributes: [:attachment], payment_option_ids: []) 
    end 

edit.html.erb

<%= form_for @project do |f| %> 
    <p> 
    <%= f.label 'Name' %> 
    <%= f.text_field :title %> 
    </p> 
    <P> 
    <%= f.label 'Budget' %> 
    <%= f.text_field :price %> 

    für 

    <%= f.select :price_category, Project::PRICE_CATEGORIES %> 
    </P> 
    <p> 
    <%= f.label 'Description' %> 
    <%= f.text_area :description %> 
    </p> 
    <p> 
    <%= f.label 'Category' %> 
    <%= f.collection_select :category_id, Category.all, :id, :name %> 
    </p> 

    <p> 
    <%= f.label 'Skills Freelancer (15 pieces) *' %> 
    <%= f.text_field :skill_list %> 
    </p> 

    <p> 
    <%= f.label 'Location' %> 
    <%= f.text_field :location %> 
    </p> 


    <ul> 
    <% @payment_types.each do |type| %> 
     <li><%= check_box_tag 'project[payment_option_ids][]', type.id %> 
     <%= type.name %> 
     </li> 
    <% end %> 
    </ul> 

    <p> 
    <b>Anonymity order</b> 
    </p> 
    Setup allows for players to remain anonymous. Note that this may reduce the number of responses. 

    <p> 
    <%= f.label 'Place Order anonymously' %> 
    <%= f.check_box :anonymity %> 
    </p> 

    <p> 
    <%= f.fields_for :documents do |d| %> 
     <%= render 'document_fields', f: d %> 
    <% end %> 
    <div class="links"> 
     <%= link_to_add_association 'add file', f, :documents %> 
    </div> 
    </p> 


    <P> 
    <%= f.submit 'Edit' %> 
    </P> 

<% end %> 

_document_fields.html.erb:

<div class="nested-fields"> 

    <%= f.label :attachment %> 
    <%= f.file_field :attachment %> 

    <%= link_to_remove_association "remove file", f %> 

</div> 

document.rb

class Document < ApplicationRecord 
    belongs_to :project 

    dragonfly_accessor :attachment 
end 

在編輯頁面文件的字段列表中顯示:「該文件沒有選擇」,但必須指定當前的文件。

+0

你有沒有設置你的模型是否正確?添加'''dragonfly_accessor:attachment'''到Document?添加了列,正確映射的關聯等? – SacWebDeveloper

+0

是的。 class Document

回答

0

file_field僅用於上傳新文件,不用於顯示數據庫中已存在的內容。要顯示上傳文件的列表,您應該只顯示文件的名稱和/或帶縮略圖的圖像標籤,並提供銷燬鏈接。 您還可以提供創建新附件的鏈接。

edit.html.erb

<div> 
    <ul> 
    <%= @project.documents.each do |document| %> 
    <li> 
     <%= document.attachment.name %> 
     <%= link_to "Delete", document_path(document), method: :delete, data: { confirm: 'Are you sure you want to delete this?' } %> 
    </li> 
    <% end %> 
    </ul> 
    <div class="links"> 
    <%= link_to_add_association 'add file', f, :documents %> 
</div> 
</div> 
+0

謝謝。我在方法刪除控制器documents_controller中添加了什麼? –

+0

是的,向DocumentController添加'destroy'方法。您可能需要設置授權,以便只有文檔所有者才能刪除。爲此,請查看Pundit或CanCanCan。 – SacWebDeveloper