Creating a Rails application in a single file

Updated . Posted . Visible to the public.

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:

  1. Put everything (gems, application config, database migrations, models, controllers) into a single .ru file, like app.ru.
  2. Run it via rackup app.ru. (Hint: if your file is called config.ru, you can just run rackup).
  3. 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 a Gemfile.lock. Then, run it via

    bundle 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 and bundle exec rackup.

  • Your application is running in the development environment by default. You can use RAILS_ENV=... rackup to change that.

Arne Hartherz
Last edit
Jonas Schiele
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2022-08-29 06:04)