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!

Any comments? Write me a DM on Twitter.

Before you leave…

Would you like to learn how to deploy your own Ruby on Rails applications? I am writing a book for people like you.

Check it out →