Sinatra 视图中的相对路径问题

发布于 2024-09-01 00:09:53 字数 480 浏览 2 评论 0原文

在我的 erb 文件中发布图像之前,我使用以下代码来检查文件是否存在。这是一个 ruby​​/sinatra 应用程序 - 不是 Rails。

<% @imagename = @place.name + ".jpg" %> 
<% if FileTest.exist?( "/Users/Tim/projects/game/public/" + @imagename ) %> 
<p><img src= '<%= @imagename %>' width="400" height="300" /> </p> 
<% end %> 

当我将其发布到 Heroku 时,它显然行不通。

我尝试使用相对路径,但无法让它工作:

<% if FileTest.exist?( "/" + @imagename ) %> 

I am using the following code to check existence of a file before publishing an image in my erb file. This is a ruby/sinatra app - not rails.

<% @imagename = @place.name + ".jpg" %> 
<% if FileTest.exist?( "/Users/Tim/projects/game/public/" + @imagename ) %> 
<p><img src= '<%= @imagename %>' width="400" height="300" /> </p> 
<% end %> 

And when I publish this to Heroku, it obviously won't work.

I tried using a relative path, but I'm unable to get it to work:

<% if FileTest.exist?( "/" + @imagename ) %> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-09-08 00:09:53

/ 开头的路径不是相对路径,而是绝对路径。它说转到根目录,然后向下导航到以下路径

第一步是检查您的应用程序从哪里运行。即当前目录是什么。为此,请暂时将 <%= Dir.pwd %> 放入您的视图中,并在本地和 Heroku 上尝试此操作以比较两个环境。

然后尝试从此文件夹到图像的相对路径。例如,如果应用程序从 /Users/Tim/projects/game 运行,则 public 的相对路径就是 public,因此图像的路径将为 File .join('public', @imagename)

如果您需要更多帮助,请发布两个环境中 Dir.pwd 的值


这是另一种方法:

__FILE__ 是一个特殊的 Ruby 变量,它提供当前文件的相对路径。

利用这一点,在启动应用程序的 .rb 文件中设置一个常量,如下所示:(

APP_ROOT = File.dirname(__FILE__)

应用程序的 config.rb 中的类似行用于在 Rails 应用程序中设置 RAILS_ROOT

然后在您的您可以使用的视图:

FileTest.exist?(File.join(APP_ROOT, 'public', @imagename))

A path starting with a / is not a relative path, that's an absolute path. It says go to the root and then navigate down to the following path

The first step is to check where your app is running from. i.e. what is the current directory. To do this temporarily put <%= Dir.pwd %> in your view and try this both locally and on Heroku to compare the two environments.

Then try a relative path from this folder to the image. e.g. If the app is running from /Users/Tim/projects/game then the relative path to public is just public so the path to the image would be File.join('public', @imagename)

If you need more help then please post the value of Dir.pwd from both environments


Here is another approach:

__FILE__ is a special Ruby variable that gives a relative path to the current file.

Making use of that, in the .rb file that starts your app set a constant as follows:

APP_ROOT = File.dirname(__FILE__)

(a similar line in an app's config.rb is used to set RAILS_ROOT in a Rails app)

Then in your view you can use:

FileTest.exist?(File.join(APP_ROOT, 'public', @imagename))
倾其所爱 2024-09-08 00:09:53

您想要的是使用RAILS_ROOT常量 - 它指向您的应用程序的文件夹。因此,在本地计算机上 RAILS_ROOT 将计算为 /Users/Tim/projects/game/

这是 Rails 特定的,请使用 File.dirname( __FILE__) 相反。

What you want to is use the RAILS_ROOT constant - it points to your app's folder. So on your local machine RAILS_ROOT will evaluate to /Users/Tim/projects/game/.

This is Rails specific, use File.dirname(__FILE__) instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文