Read more

Creating a Rails application in a single file

Arne Hartherz
August 29, 2022Software engineer at makandra GmbH

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.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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.

Posted by Arne Hartherz to makandra dev (2022-08-29 08:04)