Question

Assuming I have a directory structure similar to:

path_to_file/one/index.html

How can I set my sinatra app to be routed to

mysite.com/path_to_file/one/

and have the previously mentioned file to render? path_to_file will always stay the same, but there will be different folders (two, three, etc.) inside it.

I've tried the following:

get '/path_to_file/:number' do
  File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end

but then the e.g. javascript file linked from index.html doesn't render correctly.

Was it helpful?

Solution

Got it!

get '/path_to_file/:number/:file' do
  File.read(File.join('path_to_file', "#{params[:number]}", "#{params[:file]}"))
end

get '/path_to_file/:number' do
  File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end

Order is important, since if these two methods are reversed, get '/path_to_file/:number' becomes a superset of get '/path_to_file/:number/:file'.

OTHER TIPS

Just a thought, but you could setup your server software, Apache, nginx, whatever it is you're using, to serve .css and .js and image files from a different location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top