基于ruby的gem remotipart的异步上传文件

阅读更多
针对某一对象保存实例化之前,异步上传图片保存。
gem 'remotipart', '~> 1.2'


//= require jquery.remotipart



添加知识

<%= render 'form', url: admin_knowledge_libraries_path, meth: :post %> <% content_for :scripts do %> <% end %>


  def add_document
    new_attachment = ::Attachment.create({attachment_entity_type: ::Knowledge::Library.name, path: params[:draft_img], created_by: CreatedBy.format_created_by(current_employee)})
    had_attachement_ids = params[:had_attachement_ids].present? ? params[:had_attachement_ids].split(",") : []
    @had_attachement_ids = had_attachement_ids << new_attachment.id

    @new_problem_message_images = ::Attachment.where(id:@had_attachement_ids)
    @attachement_ids = @had_attachement_ids.join(",")

    respond_to do |format|
      format.js
    end
  end


<%= render partial: "message_image_list", locals: { message_image_list: @new_problem_message_images} %>

<%= form_for @library, :method=>"post", :remote=>true , :url=>"/admin/knowledge/libraries/add_document", html: { id: 'image_preview_form',:multipart => true,autocomplete: "off" } do |f| %> 文档附件: <%= file_field_tag "draft_img", id: 'draft_img' %> <%= hidden_field_tag :had_attachement_ids,@attachement_ids %> <% end %>
<%= simple_form_for @library, url: url, method: meth do |f| %>
<%= f.input :title %>
<%= f.input_field :started_at, as: :string %>
<%= f.input :source %> <%= f.input :source_url %>
<%= hidden_field_tag :commit_attachement_ids,@attachement_ids %>
<% end %>


  $("#image_preview").html('<%= j(render partial: "message_image_list", locals: { message_image_list: @new_problem_message_images} ) %>');
  $("#commit_attachement_ids").val('<%=@attachement_ids%>')
  $("#had_attachement_ids").val('<%=@attachement_ids%>')


   <% if message_image_list %>
   
   <% message_image_list.each_with_index do |att,i| %>
        <% if i == 0 %>
          
        <% elsif (i >= 10 && i%10 == 0) %>
          
        <% end %>
          
   <% end %>
   
<%if att&.content_type&.include?("image")%> <%= image_tag att.path, width: '100px', heigh: '50px', title: att.name,style:"margin:5px;" %> <% else%>
<%= att.name %>
<% end %>
<% end %>

你可能感兴趣的:(基于ruby的gem remotipart的异步上传文件)