Skip to content

How to display the file name of an upload in Ruby on Rails

Posted on 04 January 2007

Often you want to display just the filename of an upload/attachment that a user has uploaded. Just do the following:

File.basename(@vacancy.document)

The path will be stripped out and you are left with just the filename and extension.

If you don't want the extension (suffix) to be output you could do:

File.basename(@vacancy.document, File.extname(@vacancy.document))

Basically if you pass in the extension then it will not be returned.

File.extname just returns the extension (the portion of file name in path after the period)

I often combine this with File.size to show the user both the format of the upload and the file size

View the snippets archive ››

No comments have been left

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com