Ruby on rails 我无法下载使用carrierwave上传的文件
以前我可以下载我上传的单个文件,但当我把它改为多个文件上传时,我无法下载请帮助我 这是我的控制器 发票\u详细信息\u controller.rbRuby on rails 我无法下载使用carrierwave上传的文件,ruby-on-rails,carrierwave,Ruby On Rails,Carrierwave,以前我可以下载我上传的单个文件,但当我把它改为多个文件上传时,我无法下载请帮助我 这是我的控制器 发票\u详细信息\u controller.rb class InvoiceDetailsController < ApplicationController before_action :set_invoice_detail, only: [:show, :edit, :update, :destroy] respond_to :html de
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 %>
类InvoiceDetailsController@invoice_detail}
format.pdf{render:layout=>false}
终止
终止
def新
@发票详细信息=发票详细信息.new
回复(@invoice\u detail)
终止
定义编辑
终止
def创建
@发票详细信息=发票详细信息。新建(发票详细信息参数)
回应待办事项|格式|
如果@invoice\u detail.save
format.html{将\重定向到发票\详细信息\路径,注意:'发票已成功创建。}
其他的
format.html{呈现操作:“新建”}
终止
终止
终止
def更新
@发票详细信息。更新(发票详细信息参数)
将\u重定向到发票\u详细信息\u路径
终止
def销毁
@销毁发票
将\u重定向到发票\u详细信息\u路径
终止
def下载
回应待办事项|格式|
format.html{
如果参数为[:发票号].nil?
重定向到:返回
其他的
开始
发票=InvoiceDetail.find(参数[:发票id])
附件文件=file.join('public',invoice.attachment.url)
如果文件存在?(附件文件)
发送\u文件附件\u文件,:disposition=>“附件”
其他的
重定向到:返回
终止
rescue异常=>错误
重定向到:返回
终止
终止
}
终止
终止
私有的
def设置\u发票\u详细信息
@发票详细信息=发票详细信息.find(参数[:id])
终止
def发票详细信息参数
参数要求(:发票详细信息)。许可(:附件,:发票编号,:供应商名称,:发票日期,:发票到期日期,:货物描述,:数量,:单价,:总金额,:付款方式,:状态,:装运国家)
终止
终止
这是我的模型
发票详细信息.rb
类InvoiceDetail
'控件标签'%>
'text_field',:required=>true,:maxlength=>15,:placeholder=>'15带特殊字符的字母数字',:presence=>“check invoice num”%>
'控件标签'%>
“text_field',:required=>true,:maxlength=>20,:placeholder=>20个字母数字字符'%>
'控件标签'%>
'文本区域'%>
'控件标签'%>
'文本区域'%>
'控件标签'%>
“text_area',:maxlength=>50,:placeholder=>”最多可接受50个带有特殊字符“%”的字母数字
'控件标签'%>
“text_area',:maxlength=>10,:placeholder=>”最多可接受10位“%”
'控件标签'%>
“text_area',:maxlength=>15,:placeholder=>”最多可接受15位“%”
'控件标签'%>
“text_area',:maxlength=>15,:placeholder=>”最多可接受15位“%”
'控件标签'%>
'文本\区域',:占位符=>'选择付款方式'%>
'控件标签'%>
'文本区域'%>
'控件标签'%>
'文本区域'%>
'控件标签'%>
true,name:“附件[avatar][]”,:class=>file\u field,:required=>true%>
“btn btn主节点”%>
'btn btn danger',方法::删除,数据:{确认:'是否确实要取消'}%>
禁止保存此发票详细信息:
'控件标签'%>
'text_field',:required=>true,:maxlength=>15,:placeholder=>'15带特殊字符的字母数字',:presence=>“check invoice num”%>
'控件标签'%>
“text_field',:required=>true,:maxlength=>20,:placeholder=>20个字母数字字符'%>
'控件标签'%>
'文本区域'%>
'控件标签'%>
'文本区域'%>
'控件标签'%>
“text_area',:maxlength=>50,:placeholder=>“最多可接受50个带有特殊字符的字母数字”
def download_video
@video = Video.find params[:id]
redirect_to @video..avatar.expiring_url(10)
end