如何让 Sinatra 在每次更改后自动重新加载文件?

发布于 2024-07-30 18:43:23 字数 179 浏览 2 评论 0原文

我正在使用

# my_app.rb
load 'index.rb'

并启动这样的服务器

ruby my_app.rb

,但它永远不会重新加载我在索引页面中所做的任何更改。
我在这里错过了什么吗?

I am using

# my_app.rb
load 'index.rb'

and start the sever like this

ruby my_app.rb

but it never reload any changes I made in index page.
Did I miss anything here?

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

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

发布评论

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

评论(9

影子是时光的心 2024-08-06 18:43:23

请参阅 Sinatra 常见问题解答

"如何让我的 Sinatra 应用程序在更改时重新加载?"

首先,在 Ruby 中重新加载进程内代码很困难并且有一个
适用于所有场景的解决方案在技术上是不可能的。

这就是为什么我们建议您进行进程外重新加载。

首先,您需要安装重新运行(如果尚未安装):

 $ gem install 重新运行 
  

现在,如果您像这样启动 Sinatra 应用程序:

$ ruby​​ app.rb 
  

重新加载所需要做的就是执行以下操作:

$ 重新运行 'ruby app.rb' 
  

如果您正在使用rackup,请改为
以下内容:

$ 重新运行“rackup”

你明白了。

如果您仍然想要进程内重新加载,请查看 Sinatra::Reloader .

See the Sinatra FAQ,

"How do I make my Sinatra app reload on changes?"

First off, in-process code reloading in Ruby is hard and having a
solution that works for every scenario is technically impossible.

Which is why we recommend you to do out-of-process reloading.

First you need to install rerun if you haven’t already:

 $ gem install rerun

Now if you start your Sinatra app like this:

$ ruby app.rb

All you have to do for reloading is instead do this:

$ rerun 'ruby app.rb'

If you are for instance using rackup, instead do
the following:

$ rerun 'rackup'

You get the idea.

If you still want in-process reloading, check out Sinatra::Reloader.

镜花水月 2024-08-06 18:43:23

gem 安装 sinatra-reloader

require 'sinatra'
require 'sinatra/reloader'

注意:它只会重新加载 sinatra 处理程序(也许还有一些 sinatra 服务器配置命令),但不会重新加载自定义文件,您必须手动重新加载这些文件。

9年后的UPD:似乎已经可以使用also_reloaddont_reloadafter_reload重新加载其他文件 - - https://github.com/sinatra/sinatra/pull/1150

gem install sinatra-reloader

require 'sinatra'
require 'sinatra/reloader'

Note: it will reload only sinatra handlers (and, maybe some sinatra server configuration commands), but not custom files, which you have to reload manually.

UPD after 9 years: seems like it is already possible to reload other files using also_reload, dont_reload and after_reload -- https://github.com/sinatra/sinatra/pull/1150

み零 2024-08-06 18:43:23

您可以使用rerun gem。

gem install rerun
rerun 'ruby app.rb' 

或者如果您使用的是rackup

rerun 'rackup'

You can use the rerun gem.

gem install rerun
rerun 'ruby app.rb' 

OR if you are using rackup

rerun 'rackup'
蓝色星空 2024-08-06 18:43:23

gem install sinatra-reloader

require 'sinatra/base'
require "sinatra/reloader"

class MyApp < Sinatra::Base
  register Sinatra::Reloader

  get '/' do
    "Hello Testing1!"
  end
end

您可能需要将环境变量设置为开发并有条件地加载 gem。

gem install sinatra-reloader

require 'sinatra/base'
require "sinatra/reloader"

class MyApp < Sinatra::Base
  register Sinatra::Reloader

  get '/' do
    "Hello Testing1!"
  end
end

You may want to set environment variable to development and conditionally load the gem.

无敌元气妹 2024-08-06 18:43:23

当您使用 Passenger Standalone 运行应用程序时,只需创建一个 tmp/always_restart 文件:

$ touch tmp/always_restart.txt

请参阅 Passenger 文档了解更多信息。

When you run the application with Passenger Standalone, just create a tmp/always_restart file:

$ touch tmp/always_restart.txt

See Passenger documentation for more info.

放我走吧 2024-08-06 18:43:23

我喜欢霰弹枪宝石。 如果您使用模块化 Sinatra 应用程序并拥有 config.ru 文件,则很容易运行。

shotgun config.ru

此处查看该 gem。 它相当简单,无需配置。

I like the Shotgun gem. If you're using a modular Sinatra app and have a config.ru file it's easy to run.

shotgun config.ru

Check the gem out here. It's fairly straight forward and no configuration needed.

流星番茄 2024-08-06 18:43:23

在 Windows 上,我使用 restart gem 来实现此目的:

restart ruby my_app.rb

或者,使用rackup:

restart rackup

请参阅 此处了解更多信息,希望您发现它有用。

On Windows, I am using my restart gem for this:

restart ruby my_app.rb

or, with rackup:

restart rackup

See here for more info, hope you find it useful.

情何以堪。 2024-08-06 18:43:23

你可以使用防护架。 摘自 dblock.org 上的文章

添加此内容到您的 Gemfile:

group :development do
  gem "guard"
  gem "guard-bundler"
  gem "guard-rack"
end

然后,在项目的根目录中创建一个包含以下内容的 Guardfile

guard 'bundler' do
  watch('Gemfile')
end

guard 'rack' do
  watch('Gemfile.lock')
  watch(%r{^(config|app|api)/.*})
end

最后,运行 Guard,如下所示:bundle execguardrackup 每次都会重新加载。

You could use guard-rack. Lifted from an article at dblock.org:

Add this to your Gemfile:

group :development do
  gem "guard"
  gem "guard-bundler"
  gem "guard-rack"
end

Then, create a Guardfile at the root of your project with this content:

guard 'bundler' do
  watch('Gemfile')
end

guard 'rack' do
  watch('Gemfile.lock')
  watch(%r{^(config|app|api)/.*})
end

Lastly, run Guard, like so: bundle exec guard, and rackup will reload every time.

⒈起吃苦の倖褔 2024-08-06 18:43:23

如果您只更改模板,那么如果您将环境设置为开发,sinatra 将始终重新渲染它们:

ruby app.rb -e development

If you only change your templates sinatra will always rerender them if you set your environment to development:

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