[英]Changing the file name that we upload using carrierwave in rails
i am using carrierwave for uploading and downloading the files. 我正在使用carrierwave上传和下载文件。 This is my model:
这是我的模特:
class InvoiceDetail < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
validates :invoice_number, :supplier_name, :attachment, presence: true
validates_uniqueness_of :invoice_number
end
attachment/uploader.rb: 附接/ uploader.rb:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
invoice_detail/_form.html.erb: invoice_detail / _form.html.erb:
<%= form_for(@invoice_detail , html: {class: 'form-horizontal', role: 'form' }) do |f| %>
<div class="field">
<%= f.label :invoice_number, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :invoice_number, :class => 'text_field' %>
</div>
</div>
<div class="field">
<%= f.label :supplier_name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :supplier_name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :attachment , :class => 'control-label' %>
<div class="controls">
<%= f.file_field :attachment, :class => 'file_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit "Create Invoice Details", :class => 'btn btn-primary' %>
<%= f.submit "cancel", :class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure you want to cancel' } %>
</div>
<% end %>
The file which I upload is stored in public/uploads folder of my rails application. 我上传的文件存储在我的rails应用程序的public / uploads文件夹中。 Now my requirement is to upload the file with invoice_number i am entering in the form and when I click on download, the file has to be downloaded with same Invoice number.
现在我的要求是上传我在表格中输入的invoice_number文件,当我点击下载时,必须使用相同的发票编号下载该文件。 (For example, i am entering my invoice number as :1234 and uploading file with name example.xlsx, the uploaded file has to be stored as 1234.xlsx) Please help me out.
(例如,我输入的发票号码为:1234并上传名为example.xlsx的文件,上传的文件必须存储为1234.xlsx)请帮帮我。
You can rename file during upload : 您可以在上传期间重命名文件 :
def filename
"#{model.invoice_number}.#{file.extension}" if original_filename.present?
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.