i have this simple script to show all files in a folder, it works in the console but gives a different result in Sinatra (with path and extension). Why is this so, and how can i best present these basenames (without path and extension) in a ul list as a link to open this file in the browser using Sinatra ?
The goal is to present a clickable list of pages to open if no filename is given. I allready have the routine to show the files.
console:
require 'find'
def get_files path
dir_array = Array.new
Find.find(path) do |f|
dir_array << f if !File.directory?(f) # add only non-directories
end
return dir_array
end
for filename in get_files 'c:/sinatra_wiki/views'
basename = File.basename(filename, ".*")
puts basename
end
=> index
index2
Sinatra:
require 'find'
def get_files path
dir_array = Array.new
Find.find(path) do |f|
dir_array << f if !File.directory?(f) # add only non-directories
end
return dir_array
end
get '/' do
for filename in get_files 'c:/sinatra_wiki/views'
basename = File.basename(filename, ".*")
puts basename
end
end
=> c:/sinatra_wiki/views/index.htmlc:/sinatra_wiki/views/index2.erb
In your sinatra implementation, the result you see in the browser is not the one from the
puts basenamestatement in thegetblock. It’s the return value of theget_filesmethod. Try addingputs "<p>#{base name}</p>"instead of theputs basenamein thegetblock and see for yourself.Some changes:
The
get_filesmethod: Instead of sending the entire file path, send only the file nameAdd a view in case you need clarity:
elsewhere, in the app/views folder, in an index.erb file:
This is to list out the file names in a similar way to that of the console output.
TL;DR: Put the looping part in the view!