ruby-on-railsnested-attributesdragonfly-gem

Gem dragonfly. How to make the list of current files on edit view?


I have model Project and in model have field documents(files). I am using gem dragonfly. When editing a project, how to make the list of current files? At this time on the edit page the list of files fields show: "The file is not selected". I do next: ​

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

On the edit page the list of files fields show: "The file is not selected", but must be specified ​current files.


Solution

  • file_field is for uploading new files only, not for displaying what is already in the database. For displaying a list of uploaded files, you should just display the name of the file and/or an image tag with the thumbnail and provide a destroy link. You can also provide a link to create a new attachment.

    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>