During the process of upgrading a project from Rails 2.2.2 to Rails 2.3.2 several of our tests were breaking with the error:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
This error was most commonly occurring in model specs where we had mixed in
ActionController::UrlWriter in order to get access the named routes (e.g.
invitation_path) inside of the model class. I believe this change in a behavior is the result of this patch to Rails but I am not certain. Interestingly the code falls apart in the tests but it still works fine within the browser.
With the assistance of Adam Milligan we were able to find an acceptable way to handle setting the default_url_options in the test environment.
# app/models/invitation.rb class Invitation < ActiveRecord::Base include ActionController::UrlWriter ... end # spec/models/invitation_spec.rb describe "Invitation" do before(:all) do Invitation.default_url_options[:host] = 'localhost' end after(:all) do Invitation.default_url_options[:host] = nil end ... end
As I wrap up I want take a moment a properly shame myself for generating urls in the model. There is definitely a good argument that you should not be using named_routes in your models and I am eager to agree. Rails makes it hard to do for a reason and if you find yourself ever explicitly including UrlWriter take a step back and think the problem over. You may find yourself needlessly going down the wrong path and a different approach is in order.
About the Author