我无法下载使用carrierwave上传的文件

时间:2015-03-18 11:39:07

标签: ruby-on-rails carrierwave

之前我能够下载whwn我上传的单个文件,但是当我将其更改为多个文件上传时我无法下载请帮帮我

这是我的控制器

invoice_details_controller.rb

    class InvoiceDetailsController < ApplicationController
      before_action :set_invoice_detail, only: [:show, :edit, :update, :destroy]

      respond_to :html

      def index
        @invoice_details = InvoiceDetail.all
        respond_with(@invoice_details)
      end

      def show
     @invoice_detail = InvoiceDetail.find(params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @invoice_detail }
        format.pdf { render :layout => false }
      end
      end

      def new
        @invoice_detail = InvoiceDetail.new
        respond_with(@invoice_detail)
      end

      def edit
      end

      def create
        @invoice_detail = InvoiceDetail.new(invoice_detail_params)
        respond_to do |format|
          if @invoice_detail.save
            format.html { redirect_to invoice_details_path, notice: 'Invoice was successfully created.' }
          else
            format.html { render action: "new" }
          end
        end
      end

      def update
        @invoice_detail.update(invoice_detail_params)
        redirect_to invoice_details_path
      end

      def destroy
        @invoice_detail.destroy
      redirect_to invoice_details_path
      end

      def download
          respond_to do |format|
            format.html {
                if params[:invoice_id].nil?
                    redirect_to :back
                else
                  begin
                    invoice = InvoiceDetail.find(params[:invoice_id])
                    attachment_file = File.join('public', invoice.attachment.url)
                    if File.exists?(attachment_file)
                      send_file attachment_file, :disposition => 'attachment'
                    else
                      redirect_to :back
                    end
                  rescue Exception => error
                    redirect_to :back
                  end
                end
            }
          end
      end

      private
        def set_invoice_detail
          @invoice_detail = InvoiceDetail.find(params[:id])
        end

        def invoice_detail_params
          params.require(:invoice_detail).permit(:attachment,:invoice_number, :supplier_name, :invoice_date, :invoice_due_date, :description_of_goods, :quatity, :price_per_unit, :total_amount, :mode_of_payment, :status, :shipping_country)
        end
    end
here are my models

invoice_details.rb

    class InvoiceDetail < ActiveRecord::Base
       has_many :attachment
       accepts_nested_attributes_for :attachment
    end

invoice_files.rb

    class InvoiceFile < ActiveRecord::Base
       mount_uploader :avatar, AvatarUploader
       belongs_to :InvoiceDetail
       validates :name, presence: true # Make sure the owner's name is present.
    end


here is my form

_form.html.erb

    <%= form_for(@invoice_detail , html: {class: 'form-horizontal', role: 'form' }) do |f| %>
      <% if @invoice_detail.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@invoice_detail.errors.count, "error") %> prohibited this invoice_detail from being saved:</h2>

          <ul>
          <% @invoice_detail.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

        <div class="field">
            <%= f.label :invoice_number, :class => 'control-label' %>
                <div class="controls">
        <%= f.text_field :invoice_number, :class => 'text_field', :required => true, :maxlength => 15, :placeholder =>'15 Alpha Numeric with special Characters', :presence => "check invoice num"   %>
      </div>
      </div>
        <div class="field">
        <%= f.label :supplier_name, :class => 'control-label' %>
                    <div class="controls">

        <%= f.text_field :supplier_name, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => '20 Alpha numeric characters'  %>
        </div>
      </div>
      <div class="field">
        <%= f.label :invoice_date, :class => 'control-label' %>
                    <div class="controls1">

        <%= f.date_select :invoice_date, :class => 'text_area' %>
        </div>
      </div>
      <div class="field">
        <%= f.label :invoice_due_date, :class => 'control-label' %>
                    <div class="controls1">

        <%= f.date_select :invoice_due_date, :class => 'text_area' %>
        </div>

      </div>
      <div class="field">
        <%= f.label :description_of_goods, :class => 'control-label' %>
                        <div class="controls">

        <%= f.text_area :description_of_goods, :class => 'text_area', :maxlength => 50, :placeholder => 'Accepts up to 50 Alpha Numeric with special Characters'%>
        </div>
      </div>
      <div class="field">
        <%= f.label :quality, :class => 'control-label' %>
                        <div class="controls">

        <%= f.text_field :quatity , :class => 'text_area',:maxlength => 10, :placeholder =>'Accepts up to 10 Digits'%>
        </div>
      </div>
      <div class="field">
        <%= f.label :price_per_unit, :class => 'control-label' %>
                        <div class="controls">

        <%= f.text_field :price_per_unit, :class => 'text_area',:maxlength => 15,  :placeholder =>'Accepts up to 15 Digits' %>
        </div>
      </div>
      <div class="field">
        <%= f.label :total_amount, :class => 'control-label' %>
                        <div class="controls">

        <%= f.text_field :total_amount, :class => 'text_area', :maxlength => 15, :placeholder =>'Accepts up to 15 Digits'  %>
        </div>
      </div>
      <div class="field">
        <%= f.label :mode_of_payment, :class => 'control-label' %>
                        <div class="controls">

        <%= f.select :mode_of_payment, options_for_select(%w[Cash Cheque Transfer LC]), :class => 'text_area', :placeholder => 'Select mode of payment' %>
        </div>
      </div>
      <div class="field">
        <%= f.label :status, :class => 'control-label' %>
                            <div class="controls">

        <%= f.select :status, options_for_select(%w[Paid Pending]), :class => 'text_area' %>
        </div>
      </div>
      <div class="field">
        <%= f.label :shipping_country, :class => 'control-label' %>
                            <div class="controls">

        <%= f.country_select :shipping_country, {priority: %w(ID US CA), prompt: 'Please select a country'}, :class => 'text_area' %>
        </div>
      </div>
         <div class="control-group">
          <%= f.label :attachment , :class => 'control-label' %>
          <div class="controls">
            <%= f.file_field :avatar, :multiple => true, name: "attachment[avatar][]", :class => 'file_field', :required => true %>
          </div>
        </div>
      <div class="form-actions1">
        <%= f.submit  :class => 'btn btn-primary' %>
        <%#= f.submit "cancel", :class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure you want to cancel' } %>

<%= form_for(@invoice_detail , html: {class: 'form-horizontal', role: 'form' }) do |f| %>
  <% if @invoice_detail.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@invoice_detail.errors.count, "error") %> prohibited this invoice_detail from being saved:</h2>

      <ul>
      <% @invoice_detail.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

    <div class="field">
        <%= f.label :invoice_number, :class => 'control-label' %>
            <div class="controls">
    <%= f.text_field :invoice_number, :class => 'text_field', :required => true, :maxlength => 15, :placeholder =>'15 Alpha Numeric with special Characters', :presence => "check invoice num"   %>
  </div>
  </div>
    <div class="field">
    <%= f.label :supplier_name, :class => 'control-label' %>
                <div class="controls">

    <%= f.text_field :supplier_name, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => '20 Alpha numeric characters'  %>
    </div>
  </div>
  <div class="field">
    <%= f.label :invoice_date, :class => 'control-label' %>
                <div class="controls1">

    <%= f.date_select :invoice_date, :class => 'text_area' %>
    </div>
  </div>
  <div class="field">
    <%= f.label :invoice_due_date, :class => 'control-label' %>
                <div class="controls1">

    <%= f.date_select :invoice_due_date, :class => 'text_area' %>
    </div>

  </div>
  <div class="field">
    <%= f.label :description_of_goods, :class => 'control-label' %>
                    <div class="controls">

    <%= f.text_area :description_of_goods, :class => 'text_area', :maxlength => 50, :placeholder => 'Accepts up to 50 Alpha Numeric with special Characters'%>
    </div>
  </div>
  <div class="field">
    <%= f.label :quality, :class => 'control-label' %>
                    <div class="controls">

    <%= f.text_field :quatity , :class => 'text_area',:maxlength => 10, :placeholder =>'Accepts up to 10 Digits'%>
    </div>
  </div>
  <div class="field">
    <%= f.label :price_per_unit, :class => 'control-label' %>
                    <div class="controls">

    <%= f.text_field :price_per_unit, :class => 'text_area',:maxlength => 15,  :placeholder =>'Accepts up to 15 Digits' %>
    </div>
  </div>
  <div class="field">
    <%= f.label :total_amount, :class => 'control-label' %>
                    <div class="controls">

    <%= f.text_field :total_amount, :class => 'text_area', :maxlength => 15, :placeholder =>'Accepts up to 15 Digits'  %>
    </div>
  </div>
  <div class="field">
    <%= f.label :mode_of_payment, :class => 'control-label' %>
                    <div class="controls">

    <%= f.select :mode_of_payment, options_for_select(%w[Cash Cheque Transfer LC]), :class => 'text_area', :placeholder => 'Select mode of payment' %>
    </div>
  </div>
  <div class="field">
    <%= f.label :status, :class => 'control-label' %>
                        <div class="controls">

    <%= f.select :status, options_for_select(%w[Paid Pending]), :class => 'text_area' %>
    </div>
  </div>
  <div class="field">
    <%= f.label :shipping_country, :class => 'control-label' %>
                        <div class="controls">

    <%= f.country_select :shipping_country, {priority: %w(ID US CA), prompt: 'Please select a country'}, :class => 'text_area' %>
    </div>
  </div>
     <div class="control-group">
      <%= f.label :attachment , :class => 'control-label' %>
      <div class="controls">
        <%= f.file_field :avatar, :multiple => true, name: "attachment[avatar][]", :class => 'file_field', :required => true %>
      </div>
    </div>
  <div class="form-actions1">
    <%= f.submit  :class => 'btn btn-primary' %>
    <%#= f.submit "cancel", :class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure you want to cancel' } %>
    <%= link_to "Cancel", invoice_details_path(), :class => 'btn btn-danger', data: { confirm: 'Are you sure you want to cancel' } %>

  </div>
<% end %>
      <%= link_to "Cancel", invoice_details_path(), :class => 'btn btn-danger', data: { confirm: 'Are you sure you want to cancel' } %>

      </div>
    <% end %>

1 个答案:

答案 0 :(得分:0)

你只需要一个有效的链接来下载任何资产。对于回形针,这就是我下载资产的方式(图像/音频/视频)

def download_video
  @video = Video.find params[:id]
  redirect_to @video..avatar.expiring_url(10)

end