使用CarrierWave on Rails上传文件后刷新页面

时间:2018-03-25 14:53:06

标签: ruby-on-rails model-view-controller carrierwave

我正在开发一个使用CarrierWave作为文件管理器(上传和下载)的应用程序。它用在我的项目模型上。它有点工作,因为它上传文件,但它没有刷新页面,也没有显示错误,如果有(但在控制台中显示 - 白名单错误),在另一种情况下(detroy)它显示消息正确。如果你们帮助我,我会非常感激,因为我测试了几件事(重定向到root等),没有任何反应。

Project.rb

class Project < ApplicationRecord

  mount_uploaders :attachments, AttachmentUploader # Tells raisl to use this uploader with this model
  serialize :attachments, JSON # If you use SQLite, add this line.

end

attachments_controller.rb

class AttachmentsController < ApplicationController
    before_action :set_project

    def create
        add_more_attachments(attachments_params[:attachments])

            respond_to do |format|
          if @project.save
            format.html { redirect_to project_path(@project), notice: 'Archivo fue subido existosamente.' }

          else
            format.html { render partial: 'projects/uploads/new' }

            format.json { render json: @project.errors, status: :unprocessable_entity }

          end
            end

    end

    def download
        path = "uploads/#{params[:id]}/#{params[:basename]}.#{params[:extension]}"
        send_file path, :x_sendfile=>true
    end

    def destroy
        remove_attachments_at_index(params[:id].to_i) 
        respond_to do |format|
            format.html { redirect_to project_path(@project), notice: 'Archivo fue eliminado existosamente.' }
        end
    end

    private

    def set_project
        @project = Project.find_by_id(params[:project_id])
    end

    def add_more_attachments(new_attachments)
        attachments = @project.attachments # copy the old images 
        attachments += new_attachments # concat old images with new ones
        @project.attachments = attachments # assign back
    end

    def remove_attachments_at_index(index)
        if @project.attachments.count == 1
            #truco para remover ultimo elemento
            @project.remove_attachments!
        else
            remain_attachments = @project.attachments # copy the array
            deleted_attachment = remain_attachments.delete_at(index) # delete the target image
            @project.attachments = remain_attachments # re-assign back
        end
        @project.save
    end

    def attachments_params
    params.require(:project).permit({attachments: []}) # allow nested params as array
    end
end

项目#显示(我上传和删除文件的地方)

<div class="medium-8 columns">
    <h3>
        Archivos
    </h3>
    <ul> 
        <% if @project.attachments.present? %>
            <% @project.attachments.each_with_index do |attachment,index| %>
                <li>
                    <%= link_to "Descargar #{attachment.file.filename}", "/uploads/#{@project.id}/#{File.basename(attachment.url)}"%>

                </li>
                <%= link_to "Eliminar", project_attachment_path(@project, index), :method => :delete, data: { confirm: "¿Está seguro de eliminar este archivo?" } %>
            <% end %>
        <% end %>
    </ul>  
    <%= render(:partial => 'projects/uploads/new') %>   

</div>

_new.html.erb(附件表单)

<div class="">
  <%= form_with(model: @project, :html => {multipart: true}, url: project_attachments_path(@project), method: :post)  do |form| %>

    <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
      <ul>
      <% @project.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

    <%= form.label :attachments %>
    <%= form.file_field :attachments, multiple: true %>
    <%= form.submit "Subir", :class => "button" %>
  <% end %>
</div>

控制台输出

Started POST "/projects/3/attachments" for 127.0.0.1 at 2018-03-25 11:51:26 -0300
Processing by AttachmentsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LW7yFaDtZqPSglxM8TSHy2FLBPIrhSl8Gn9rOkZheOWPlj6QDrGOPATlYOj2uQ5exMHp+T+iPbB8lehjY/IzqA==", "project"=>{"attachments"=>[#<ActionDispatch::Http::UploadedFile:0x007fecafc5b900 @tempfile=#<Tempfile:/tmp/RackMultipart20180325-24920-hgufys.pdf>, @original_filename="CAMBIO_ACUMULADOR_RADISSON(1).pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"project[attachments][]\"; filename=\"CAMBIO_ACUMULADOR_RADISSON(1).pdf\"\r\nContent-Type: application/pdf\r\n">]}, "commit"=>"Subir", "project_id"=>"3"}
  Project Load (0.4ms)  SELECT  `projects`.* FROM `projects` WHERE `projects`.`id` = 3 LIMIT 1
   (0.2ms)  BEGIN
  Estimate Load (0.4ms)  SELECT  `estimates`.* FROM `estimates` WHERE `estimates`.`id` = 3 LIMIT 1
  SQL (0.6ms)  UPDATE `projects` SET `attachments` = '[\"CV_Rodrigo_Cordova.pdf\",\"CAMBIO_ACUMULADOR_RADISSON_1_.pdf\"]', `updated_at` = '2018-03-25 11:51:26' WHERE `projects`.`id` = 3
   (2.1ms)  COMMIT
Redirected to http://localhost:3000/projects/3
Completed 302 Found in 16ms (ActiveRecord: 3.7ms)

然后......开始获取但浏览器没有差异,除非我手动刷新

Started GET "/projects/3" for 127.0.0.1 at 2018-03-25 11:51:26 -0300
Processing by ProjectsController#show as JS
  Parameters: {"id"=>"3"}
  Project Load (0.5ms)  SELECT  `projects`.* FROM `projects` WHERE `projects`.`id` = 3 LIMIT 1
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.5ms)  SELECT `users`.* FROM `users` INNER JOIN `projects_users` ON `users`.`id` = `projects_users`.`user_id` WHERE `projects_users`.`project_id` = 3
  Rendering projects/show.html.erb within layouts/application
  Estimate Load (0.4ms)  SELECT  `estimates`.* FROM `estimates` WHERE `estimates`.`id` = 3 LIMIT 1
   (0.4ms)  SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 0
   (0.4ms)  SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 1
   (0.4ms)  SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 2
   (0.3ms)  SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 3
  Rendered projects/uploads/_new.html.erb (1.9ms)
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Rendered projects/show.html.erb within layouts/application (15.3ms)
  Rendered layouts/_navigation_links.html.erb (0.4ms)
  Rendered layouts/_nav_links_for_auth.html.erb (1.1ms)
  Rendered layouts/_navigation.html.erb (9.8ms)
  Rendered layouts/_messages.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 139ms (Views: 124.5ms | ActiveRecord: 3.7ms)

修改

提交按钮但未对列出的文件进行任何更改

Before Refresh

手动刷新(有效!)

After Refresh

提前致谢。

2 个答案:

答案 0 :(得分:0)

你的图像div在哪里?

<div class="medium-8 columns">
    <h3>
        Archivos
    </h3>
    <ul> 
        <% if @project.attachments.present? %>
            <% @project.attachments.each_with_index do |attachment,index| %>
                <li>
                    <%= link_to "Descargar #{attachment.file.filename}", "/uploads/#{@project.id}/#{File.basename(attachment.url)}"%>

                </li>
                <%= link_to "Eliminar", project_attachment_path(@project, index), :method => :delete, data: { confirm: "¿Está seguro de eliminar este archivo?" } %>
            <% end %>
        <% end %>
    </ul>  
    <%= render(:partial => 'projects/uploads/new') %>   

</div>

类似这样的事情

<%= image_tag attachment.url %>

但你说它适用于refesh?

答案 1 :(得分:0)

我找到了解决方案。该表格由ajax提交。

所以我必须添加到我的表单local:true参数,如下所示:

<%= form_with(model: @project,local: true, :html => {multipart: true}, url: project_attachments_path(@project), method: :post)  do |form| %>
...
<% end %>

就像魅力一样。 感谢读者。