gespinha
gespinha

Reputation: 8487

Get current file name on Rails

I'm working with a set of files in a Rails project and I need to get the current file name. I'm using the following:

<%= __FILE__ %>

But it is outputting the full file path:

/Users/[myuser]/Documents/Development/welcome.html.erb

I just need the file name:

welcome

How can I do this?

Upvotes: 0

Views: 2287

Answers (2)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34318

To get the filename only, you can use File#basename method:

File.basename(__FILE__, '.html.erb') 
# => welcome

or,

File.basename(__FILE__, '.*') 
# => welcome

So, your code becomes:

<%= File.basename(__FILE__, '.html.erb') %>

Upvotes: 2

Max Williams
Max Williams

Reputation: 32933

shortname = File.basename(__FILE__, ".html.erb")

Upvotes: 3

Related Questions