Ruby-on-rails 在视图中显示 Carrierwave 文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5132847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:32:00  来源:igfitidea点击:

Displaying a Carrierwave filename in the view

ruby-on-railsruby-on-rails-3carrierwave

提问by Chris Alley

I am trying to display the filename of a Carrierwave attachment in a Rails erb template. The following does not work:

我正在尝试在 Rails erb 模板中显示 Carrierwave 附件的文件名。以下不起作用:

<%= @page.form.filename %>

This seems in line with the documentation. Is some additional step needed?

这似乎与文档一致。是否需要一些额外的步骤?

My page model looks like this:

我的页面模型如下所示:

class Page < ActiveRecord::Base

  mount_uploader :form, FormUploader

end

The form uploader looks like this:

表单上传器如下所示:

class FormUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(pdf)
  end

end

回答by kikito

I have been able to get the filename via the fileinternal parameter:

我已经能够通过file内部参数获取文件名:

<%= @page.form.file.filename %>

回答by Zachary Anker

The documentation you're looking at is the sanitized file, it's what it uses for actually storing a file. The part you're looking for is FormUploader, which is an Uploader, and part of http://rubydoc.info/gems/carrierwave/0.5.2/CarrierWave/Uploader

您正在查看的文档是经过消毒的文件,它用于实际存储文件。您正在寻找的部分是 FormUploader,它是一个上传器,以及http://rubydoc.info/gems/carrierwave/0.5.2/CarrierWave/Uploader 的一部分

If you want to get the file name, you could either read it from the database column directly, or use File.basename(@page.form.path)to extract it easily.

如果要获取文件名,可以直接从数据库列中读取,也可以使用File.basename(@page.form.path)轻松提取。

回答by epylinkn

The Carrierwave docsmight be a bit off, but recommended way seems to be:

Carrierwave文档可能有点过,但建议的方式似乎是:

@page.form.file.identifier

回答by omarvelous

@adamonduty's solution is great. Another solution I used before, just create a method on the model:

@adamonduty 的解决方案很棒。我之前使用的另一个解决方案,只需在模型上创建一个方法:

def name
  file.path.split("/").last
end

回答by skplunkerin

You're right @epylinkn. Documentation points towards using:

你是对的@epylinkn。文档指向使用:

@page.form.file.identifier

But when I use that, I always get nil(just as @Cheng commented).

但是当我使用它时,我总是得到nil(就像@Cheng 评论的那样)。

I then inspected my objects methods (@page.form.file.methods.inspect), and found the following to work:

然后我检查了我的对象方法 ( @page.form.file.methods.inspect),并发现以下方法有效:

@page.form.file_identifier

回答by Solomons_Ecclesiastes

In your model's associated uploader class, define a filename method.

在模型的关联上传器类中,定义文件名方法。

def filename
  File.basename(path)
end

You can then call

然后你可以打电话

model_instance.file.filename

Works as of CarrierWave 1.1.0. This is a succinct restatement/amalgamation of kikito and Chris Alley's responses above.

从 CarrierWave 1.1.0 开始工作。这是上面 kikito 和 Chris Alley 回答的简洁重述/合并。

回答by adamlamar

If you're using ActiveRecord, you can directly access the field named formin two ways:

如果您使用的是 ActiveRecord,则可以通过form两种方式直接访问命名字段:

def my_method
  self[:form]
end

or

或者

def my_method
  form_before_type_cast
end

The second method is read-only.

第二种方法是只读的。

回答by Winfield

I'm assuming you've got models like this?

我假设你有这样的模型?

class Page
  mount_uploader :form, FormUploader
end

If so you should be able to call:

如果是这样,您应该可以调用:

@page.form.url
@page.form.filename

Are you sure you've uploaded/attached the file correctly? What do you see when you inspect @page.form? Remember, the attachment will not be saved until you've fully processed the upload.

您确定您已正确上传/附加文件吗?当您检查@page.form 时,您看到了什么?请记住,在您完全处理完上传之前,不会保存附件。

回答by deefour

CarrierWave::SanitizedFilehas a private original_filenamemethod containing the filename of the uploaded file. (docs: http://rdoc.info/github/jnicklas/carrierwave/master/CarrierWave/SanitizedFile:original_filename)

CarrierWave::SanitizedFile有一个original_filename包含上传文件的文件名的私有方法。(文档:http://rdoc.info/github/jnicklas/carrierwave/master/CarrierWave/SanitizedFile: original_filename

After reading through this thread from the CarrierWave mailing list, none seemed to fit my needs. With something like

从 CarrierWave 邮件列表中通读此线程后,似乎没有一个适合我的需求。像这样的东西

class Upload < ActiveRecord::Base
  mount_uploader :file, FileUploader
  # ...

I heavily modify the :filecolumn value from the original filename. Due to this I decided to track the original filename in a separate column from the one bound to CarrierWave. In my FileUploaderI simply added a reader that wraps the private original_filenamemethod:

:file从原始文件名中大量修改了列值。因此,我决定在与绑定到 CarrierWave 的列不同的列中跟踪原始文件名。在我的FileUploader我只是添加了一个包装私有original_filename方法的阅读器:

def original_file
  original_filename
end

I then added a before_createevent to the Uploadclass (my Uploadrecords are never modified, so a before_createis acceptable for my needs)

然后我before_createUpload班级添加了一个事件(我的Upload记录从未被修改,所以 abefore_create可以满足我的需要)

before_create do
  self.original_file = self.file.original_file
end

回答by why

This is my solution:

这是我的解决方案:

  before_save :update_file_attributes


  def update_file_attributes
    if file.present? && file_changed? 
      self.content_type = file.file.content_type
      self.file_size = file.file.size
      self.file_name = read_attribute(:file)
    end
  end