Sinatra:在启动或重新启动我的应用程序时执行代码

发布于 2024-09-19 04:50:32 字数 165 浏览 2 评论 0原文

我想在应用程序启动时执行一些 ruby​​ 代码。实际上,这是一些用于创建我的所有表(如果它们尚不存在)的 SQL。

现在我应该手动运行我的sql(创建普通的新表),但我想在我的helloworld.rb sql中的某个地方编写,一旦我的应用程序启动或重新启动,它将执行sql。

谢谢

I want to execute some ruby code on the start of application. Actualy this is some SQL for creating all my tables if they are not already exists.

Nowaday I should run my sql manualy (creating new tables ordinary), but I want to write somewhere in my helloworld.rb sql, which will execute sql once my app is started or restarded.

Thx

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-09-26 04:50:32

您只需将其放在 get/post 方法之前即可。

此代码始终在启动时解释,并且仅在启动时解释

require 'sinatra'

puts 'I am starting'
puts 'I can do some SQL stuff'

get '/' do
  'hello'
end

此代码仅启动一次我的打印。

You just need place it before your get/post method.

This code is allways interpret in starting and only in starting

require 'sinatra'

puts 'I am starting'
puts 'I can do some SQL stuff'

get '/' do
  'hello'
end

This code launch only one times my print.

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