如何根据生产或开发环境在 Sinatra 中设置全局变量?

发布于 2024-12-27 13:13:36 字数 415 浏览 2 评论 0原文

我正在使用 Sinatra 的 set 方法来分配全局变量:

set :location, 'Melbourne'

我想更新它,以便该变量是静态的或动态的,具体取决于应用程序是处于开发阶段还是生产阶段。我在下面尝试了这个,它在开发中有效,但在生产中不起作用:

set :location, production? ? request.location.city : 'Melbourne'

request.location.city 来自地理定位 gem,并且此方法在其他情况下在生产中运行良好。 if 语句中是否缺少某些内容,或者 Sinatra set 方法不接受语句?

I'm using Sinatra's set method in order to assign a global variable:

set :location, 'Melbourne'

I want to update this so that the variable is static or dynamic depending on whether the app is in development or production. I tried this below, which works in development, but not in production:

set :location, production? ? request.location.city : 'Melbourne'

The request.location.city is from the geolocation gem, and this method work fine in production in other situations. Is there something in the if statement that I'm missing, or does the Sinatra set method not accept statements?

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

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

发布评论

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

评论(1

寒尘 2025-01-03 13:13:36

Request 在顶层不可用,只能在请求处理程序中使用。

编写一个方法而不是全局设置,例如:

def location(request)
  production? ? request.location.city : 'Melbourne'
end

Request is not available at the top level, only inside request handlers.

Write a method instead of a global setting, e.g.:

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