西纳特拉 +捆绑器?
我想知道如何将 Bundler 与 Sinatra 一起使用。这个想法是使用 Bundler 在 .gems 文件夹中下载的 gem。
I'm wondering how one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的 Sinatra 应用程序中,您只需要求捆绑程序设置:
或者,如果您不想在应用程序顶部添加额外的
require "bundler/setup"
,您可以改为调用sinatra viabundle exec
(例如bundle exec ruby myapp.rb
)这假设您的应用程序根目录中有一个
Gemfile
。它可能看起来像这样:这还假设您已经安装了捆绑器(
gem install bundler
)并且运行了bundle install
来安装所有gem依赖项。Inside your Sinatra app, you just have to require the bundler setup:
Alternatively, if you don't want to add the additional
require "bundler/setup"
at the top of your app, you can instead invoke sinatra viabundle exec
(e.g.bundle exec ruby myapp.rb
)This assumes that you have a
Gemfile
in the root of your application. It might look like this:This also assumes that you've already installed bundler (
gem install bundler
) and that you ranbundle install
to install all the gem dependencies.我相信最好的方法这里描述< /a> 在 EngineYard 博客上:
I believe the best way is described here on EngineYard blog:
由于我原来的答案很旧,但似乎仍然关注这个主题,这里是最新版本的bundler/sinatra设置,它将涵盖大部分用例:
一个最小的
config.ru
一个环境< code>env.rb 文件,需要所有捆绑的 gem(还支持加载当前环境的组):
然后是您的应用程序文件(需要环境)和您的 sinatra 应用程序 (
Sinatra::Base
):使用rackup启动你的开发服务器,Sinatra将通过Bundler加载,你的应用程序可以从http://localhost访问: 9292。
或
bundle execrackup
(如果需要)确保您有一个如下所示的 Gemfile,并且在启动应用程序之前运行
bundle
命令As my original answer was quite old but there seems to be still attention to this topic here's the latest version of bundler/sinatra setup which will cover most of the use case:
A minimal
config.ru
An environment
env.rb
file that requires all the bundled gems (also supports loading the current environment's group):Then your app file (requiring the environment) with your sinatra app (
Sinatra::Base
):Start your development server with rackup, and Sinatra will be loaded via Bundler, your app will be accessible from http://localhost:9292.
or
bundle exec rackup
if neededMake sure you have a Gemfile like the following one and you run the
bundle
command before starting the app+1 捆绑器网站上的指南,但如果您有一个简单的应用程序并在顶层,那么您需要执行以下操作:
在您的 Gemfile 中(告诉捆绑程序不需要 sinatra):
以及在应用程序的文件中(明确要求 sinatra):
+1 for the guide on the bundler website, but if you have a simple app and use Sinatra's dsl at the top level, then you need to do the following:
in your Gemfile (tell bundler not require sinatra):
and in the app's file (explicitly require sinatra):
要将捆绑器与 Sinatra 应用程序一起使用,您只需要做两件事。首先,创建一个 Gemfile。
gem 'sinatra'
然后,设置您的 config.ru 文件以在加载您的 Sinatra 应用程序之前加载该包。
使用rackup启动您的开发服务器,Sinatra将通过Bundler加载。
rackup
源捆绑器文档
To use bundler with a Sinatra application, you only need to do two things. First, create a Gemfile.
gem 'sinatra'
Then, set up your config.ru file to load the bundle before it loads your Sinatra app.
Start your development server with rackup, and Sinatra will be loaded via Bundler.
rackup
source bundler docs