gpt4 book ai didi

ruby - 有没有一种简单的方法可以引用 Sinatra 中的公共(public)目录?

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:01 24 4
gpt4 key购买 nike

想象一下这个结构:

   /project
/templates
view1.haml
view2.haml
misc_views/
view3.haml
view4.haml
even_deeper/
view5.haml
/public
script1.js

模板的深度可能会有所不同,如果我想从中包含一些文件,我想引用公共(public)目录。是否有帮助程序或其他一些技巧可以将我带到公共(public)目录?在我看来,拥有类似 ../../../public/script1.js 或 ../../public/script1.js 的内容似乎很糟糕。当然必须有更好的方法。

最佳答案

您可以使用 the settings.public configuration引用公共(public)目录。例如:

get "/" do
js = File.join( settings.public, 'script1.js' )
File.exists?( js ) #=> true
end

由于这是绝对路径,因此无需相对于您的 View 来获取文件。您可以从您的 View 中接触到它,但我个人会将路径设置为来自 Controller 的实例变量。

get "/" do
# If you really only need the directory
@public = settings.public

# If you actually need the path to a specific file
@main = File.join( settings.public, 'main.js' )

# If you actually need the contents of a file
@js = IO.read( File.join( settings.public, 'main.js' ) )
end

关于ruby - 有没有一种简单的方法可以引用 Sinatra 中的公共(public)目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4750046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com