Jay
Jay

Reputation: 6244

create a substring of a string

in my view i have the following:

<% @files.each do |f| -%>
    <% str = f.split("#{RAILS_ROOT}/public/downloads/") %>
    <% =str% ><br>
<% end -%>

each file name that is output is preceeded by "/public/downloads/". i have tried numerous functions but can't seem to find a function that works to get rid of it.

Thanks.

Upvotes: 0

Views: 433

Answers (2)

psyho
psyho

Reputation: 7212

This should work:

<% @files.each do |f| -%>
<% str = f.gsub(/^.*\//, '') %>
<% =str% ><br>
<% end -%>

Upvotes: 1

Eimantas
Eimantas

Reputation: 49344

Have you tried basename?

Pathname.new(str).basename.to_s

update:

<% @files.each do |f| -%>
    <% str = Pathname.new(f).basename.to_s %>
    <%= str %><br>
<% end -%>

Upvotes: 2

Related Questions