如何在sinatra上的目录中查看文件列表? [英] How can I view a list of my files in a directory on sinatra?

查看:52
本文介绍了如何在sinatra上的目录中查看文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Sinatra上运行了一个项目,现在它有很多文件作为子目录,因此我想查看这些文件的列表以及各自的目录.例如,我在地址栏中输入了"localhost:4567/landing/",其中着陆是某些页面的容器,但它使我看到"Sinatra不知道该内容".有没有办法做到这一点.我希望你能理解我的要求.

I run an project on Sinatra, now it has a lot of files a sub-directories, so I want to view a list of those files with their respective directory. For example, I put in the address bar "localhost:4567/landing/" where landing is the container of some pages, but it throw me this "Sinatra doesn’t know this ditty." Is there a way to do that. I hope you understand what I'm asking.

推荐答案

Sinatra不处理本地文件系统,它处理HTTP路由.例如,在 get'/'do 中的'/'是指您网站的根URL,而不是网站的根目录.为了列出本地文件,您需要使用Ruby Dir 类,如下所示:

Sinatra doesn't deal with the local file system, it deals with HTTP routes. For example, the '/' in get '/' do refers to the root url of your website, not the root directory of your website. In order to list the local files, you would need to use the Ruby Dir class, something like this:

#!/usr/bin/env ruby

require 'rubygems'
require 'sinatra'

get '/' do
   Dir.entries('.').map { |e| "<p>#{e}</p>" }
end

当然,直接使用文件系统是危险的,即使使用路由也是很危险的,因此,我建议您通读本书,以更好地了解幕后情况.小.

Of course, it's dangerous to work with the filesystem directly, even using routes, so I would recommend that you get a better understanding of what is going on behind the scenes by reading up a little.

这篇关于如何在sinatra上的目录中查看文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆