Greg Molnar has written a neat article about
creating a single-file Rails app
Show archive.org snapshot
.
This is not meant for production use but can be useful to try things out, e.g. when hunting down a bug or embedding a Rails app into the tests of a gem.
What you do is basically:
- Put everything (gems, application config, database migrations, models, controllers) into a single
.ru
file, likeapp.ru
. - Run it via
rackup app.ru
. (Hint: if your file is calledconfig.ru
, you can just runrackup
). - Visit
http://localhost:9292/
.
There are some caveats to keep in mind:
-
It will run in whatever Ruby version is active for you. That may be fine locally.
Lock to a specific version by creating a.ruby-version
or whatever you need. -
Gems and (especially) dependencies will be resolved to their latest version. I recommend creating a
Gemfile
and moving the gem definition into it so you'll have aGemfile.lock
. Then, run it viabundle exec rackup app.ru
-
You might run into Bundler errors when multiple versions of a gem are installed for your current ruby:
You have already activated rack 1.4.7, but your Gemfile requires rack 2.2.4.
That can also be resolved by using a separate
Gemfile
andbundle exec rackup
. -
Your application is running in the
development
environment by default. You can useRAILS_ENV=... rackup
to change that.