Rails Previews That Boot and Then 500
The build went green. The preview URL came back in the PR comment. You open it, the page renders, and then you click anything that touches the database and get a 500 with a stack trace you can't read because config.consider_all_requests_local is off in production mode.
That is the Rails preview failure mode worth writing down. A container that never starts is obvious — the deploy is marked failed and you go look at the log. A container that starts, serves HTML, and fails on the first ActiveRecord call looks like a working preview until a reviewer clicks something.
Below are the five settings that account for most of these, in the order they bite. All five are configuration, not code.
1. RAILS_ENV is not production unless you say so
Rails defaults to development when RAILS_ENV is unset. In a preview container that means: no precompiled assets served, config.cache_classes off, and — the one that hurts — a development database config pointing at a host that doesn't exist in the container.
Set it explicitly. PreviewDrop sets PORT, HOST, and HOSTNAME on every preview container and routes traffic to that port; it does not set RAILS_ENV for you, because guessing wrong here is worse than failing loudly.
RAILS_ENV=production
2. SECRET_KEY_BASE must exist at boot, not at build
Rails raises on startup in production mode if SECRET_KEY_BASE is missing. The trap is where you put it.
Asset precompilation runs at build time and also wants a secret. If you set the real one as a build-time value it gets baked into the image layer, which means every preview of that commit shares it and it's readable from the image. Use a throw-away for the build step and the real value at runtime:
# build stage — throw-away, never the real secret
RUN SECRET_KEY_BASE=placeholder bundle exec rails assets:precompile RAILS_ENV=production
Then put a real value in the project's environment variables. For previews, a fresh random string per project is fine — generate one with bundle exec rails secret and don't reuse your production secret.
3. RAILS_SERVE_STATIC_FILES — Rails won't serve assets in production
In production Rails assumes something in front of it (nginx, a CDN) is serving public/assets. A preview container has nothing in front of it, so precompiled CSS and JS 404 and the page renders as unstyled HTML.
RAILS_SERVE_STATIC_FILES=enabled
This is the single most common "the preview looks broken" report, and it's one variable.
4. config.hosts rejects the preview hostname
Rails 6+ blocks requests whose Host header isn't in config.hosts. In development that list is permissive; in production it's your real domain. A preview served from a different hostname gets Blocked host — a 403 with a page that looks like a Rails error, not a routing error.
Preview hostnames follow prv-<id>.previews.previewdrop.dev. Allow that shape rather than disabling the check:
# config/environments/production.rb
config.hosts << /.*\.previews\.previewdrop\.dev/
config.hosts accepts regexes alongside exact strings, so production stays locked to your domain while previews pass. Do not set config.hosts.clear — that turns off host-header protection everywhere, including production, to fix a preview.
5. The database is the one thing a preview doesn't come with
A preview runs your app's container. It does not come with a database attached, and there is no per-preview database provisioned for you. You point DATABASE_URL at something you control.
Two honest options:
- A shared development database. Cheap, and fine for UI-only previews. The cost is that two branches running migrations against it will fight, and a reviewer can see another branch's data.
- A throwaway instance per preview. More setup, but migrations are safe and the data is disposable.
Whichever you pick, decide deliberately whether db:migrate runs on boot. Against a disposable database it's convenient:
CMD sh -c "bundle exec rails db:migrate && bundle exec rails server -b 0.0.0.0 -p ${PORT:-3000} -e production"
Against a shared development database that three other branches are using, an automatic migrate on every preview boot is how you lose an afternoon.
The two that aren't configuration
Node is required for the asset pipeline. Sprockets, importmap, Webpacker and esbuild all need a JS runtime present at build time. A ruby:3.3-slim base doesn't have one; install nodejs in the build stage or assets:precompile fails with a confusing error about a missing executable.
Puma must bind 0.0.0.0. rails server binds localhost by default. Inside a container that's the container itself, so the router never reaches it. Pass -b 0.0.0.0 and read the port from PORT.
Getting the URL in the first place
None of the above needs a pipeline file. PreviewDrop reads your Gemfile, detects Rails, and builds the project. You can override the install, build, and start commands in Project Settings when your layout needs something specific.
The loop is short: connect the repo, paste SECRET_KEY_BASE, RAILS_ENV, RAILS_SERVE_STATIC_FILES and DATABASE_URL into the project's environment variables, then open a pull request. The first build compiles gems and warms the cache; later builds reuse it. When it finishes, a comment lands on the PR with the URL. Push again and the same URL updates in place. Close or merge the PR and the preview is torn down. If a build can't start the container, the deployment is marked failed with the reason shown in the dashboard rather than sitting in queued and holding one of your concurrent-preview slots.
The Rails framework page has the reference Dockerfile and the full environment-variable list if you want to compare against your own.
Test the interaction, not the page load
The check that matters is not "does the preview load." It's "does a request that hits the database come back." Open the preview, click something that runs a query, and watch the response. If it returns, all five settings above are correct. If the page loads and the click 500s, work down this list in order: RAILS_ENV, SECRET_KEY_BASE, RAILS_SERVE_STATIC_FILES, config.hosts, DATABASE_URL.
Start with the pull request that's been waiting longest. The quickstart docs walk through the full setup, or sign up and put a live URL on your next branch. The Free plan covers two concurrent previews across three projects with a 24-hour preview lifetime and 256MB per container — enough to prove the loop on a real branch. Paid plans start at $19/mo for the workspace, not per seat.
Want previews on your own pull requests? Start free — 2 concurrent previews, no credit card.