Following Rails redirects with Capybara

This a very short note on setting up Capybara to correctly follow redirects with Rails classic redirect_to @object pattern.

The problem why Capybara might not properly follow redirects in this case is that when redirect_to is called with ActiveRecord object argument an absolute URL is created and passed instead of relative one. So depending on the way of how Capybara server is started this might not work. To avoid this issue, one has to properly setup app_host and Capybara server settings (server_host and server_port):

# Your original Capybara setup
Capybara.javascript_driver = :poltergeist

# New server settings
Capybara.app_host = 'http://localhost:3002'
Capybara.server_host = 'localhost'
Capybara.server_port = '3002'

Here we use port 3002 for both port settings and Capybara redirects as expected!


I wrote a complete guide on web application deployment. Ruby with Puma, Python with Gunicorn, NGINX, PostgreSQL, Redis, networking, processes, systemd, backups, and all your usual suspects.

More →