How do I resolve "Missing host to link to! Please provide the :host parameter"? (RoR)
Solution 1
In the end I just used:
root_path
instead of:
root_url
Solution 2
The problem may be that you have not defined default_host for test environment. Define default_host inside config/environments/test.rb like this:
config.action_mailer.default_url_options = {:host => "localhost:3000"}
Solution 3
For tests like the controller tests shown in the OP's example, another setting is needed to resolve this error. Mentioned by Kesha Antonov you can instead use the Routes' default_url_options. By adding the following setting on the last line of your config/environments/test.rb (after the end), your tests will use the host given to build URLs:
Rails.application.routes.default_url_options[:host] = 'lvh.me:3000'
As mentioned by the OP in another answer, you can instead switch to a Path Named Route when the error is from a URL named route.
If you want to set the default host for mailers previews, you will want the action_mailer.default_url_options mentioned in other answers.
config.action_mailer.default_url_options = { :host => "localhost:3000" }
See Generating URLs in the Action Mailer API docs for more information on the action_mailer setting.
Solution 4
You've to set the default_url in each environment(development, test, production).
You need make these changes.
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/test.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
Related videos on Youtube
Sheldon
https://plus.google.com/+GrahameThomson https://twitter.com/#!/grahamethomson
Updated on July 24, 2022Comments
-
Sheldon almost 2 years
I'm following the RoR Tutorial and I'm stuck at Listing 9.15
I getting the following error after running 'bundle exec rspec spec/' :
1) Authentication authorization as wrong user submitting a PATCH request to the Users#update action Failure/Error: specify { expect(response).to redirect_to(root_url) } ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true # ./spec/features/authentication_pages_spec.rb:79:in `block (5 levels) in <top (required)>'
My Authentication test code is:
require 'spec_helper'
describe "Authentication", type: :request do subject { page } describe "signin page" do before { visit signin_path } it { should have_content('Sign in') } it { should have_title('Sign in') } end describe "signin" do before { visit signin_path } describe "with invalid information" do before { click_button "Sign in" } it { should have_title('Sign in') } it { should have_selector('div.alert.alert-error', text: 'Invalid') } describe "after visiting another page" do before { click_link "Home" } it { should_not have_selector('div.alert.alert-error') } end end describe "with valid information" do let(:user) { FactoryGirl.create(:user) } before { sign_in user } #it { should have_title(user.name) } it { should have_link('Profile', href: user_path(user)) } it { should have_link('Settings', href: edit_user_path(user)) } it { should have_link('Sign out', href: signout_path) } it { should_not have_link('Sign in', href: signin_path) } describe "followed by signout" do before { click_link "Sign out" } it { should have_link('Sign in') } end end end describe "authorization" do describe "for non-signed-in users" do let(:user) { FactoryGirl.create(:user) } describe "in the Users controller" do describe "visiting the edit page" do before { visit edit_user_path(user) } it { should have_title('Sign in') } end describe "submitting to the update action" do before { patch user_path(user) } specify { expect(response).to redirect_to(signin_path) } end end end describe "as wrong user" do let(:user) { FactoryGirl.create(:user) } let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") } before { sign_in user, no_capybara: true } describe "visiting Users#edit page" do before { visit edit_user_path(wrong_user) } #it { should_not have_title(full_title('Edit user')) } end describe "submitting a PATCH request to the Users#update action" do before { patch user_path(wrong_user) } specify { expect(response).to redirect_to(root_url) } end end end end
I don't know how to resolve this issue so the test passes. How do I resolve it? Could someone explain what's going wrong? (According to the tutorial the test should be passing).
-
Sheldon over 10 yearsStill getting the same error. That doesn't seem to be making any difference.
-
mhartl over 10 yearsDid you compare your code to the reference implementation? The code you listed appears to work OK on my system.
-
Jeff Finn over 9 yearsfor me it too this answer and the answer by @AmanGarg because I had changed the url from localhost:3000 in my dev environment and I never made the same change in my test env
-
emi over 9 yearshad same error. didn't change anything. just stopped the server and restarted it again. and it worked.
-
Yannick Schuchmann over 8 yearsMind to restart rails server after changing configs.
-
Kesha Antonov over 7 yearsAdd
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
in the end oftest.rb
. (Afterdo ... end
block) -
Michael Chaney over 7 yearsThis is not an answer to the question and should not have been marked as such. Please see actual answers that show how to set the default url options in your environment-specific config files. This "answer" simply causes the url to be changed to a path, e.g. instead of having "cnn.com" in your email you'll have "/". Yes, a single forward slash. Probably not what you want. This is a symptom of being more concerned with "test passing" than actual problem solving.
-
DBrown over 6 yearsThis does answer the question "How do I resolve "Missing host link to..." when using rspec, since it gets around the need for the host. Also the other answers do not offer a resolve since it's a controller test, not a mailer test.