Ruby-on-rails 如何解决“缺少要链接的主机!请提供 :host 参数”?(RoR)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18433450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:35:57  来源:igfitidea点击:

How do I resolve "Missing host to link to! Please provide the :host parameter"? (RoR)

ruby-on-railsrubyrspeccapybararuby-on-rails-4

提问by Sheldon

I'm following the RoR Tutorialand I'm stuck at Listing 9.15

我正在学习RoR 教程,但一直停留在代码清单 9.15

I getting the following error after running 'bundle exec rspec spec/':

运行'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'

需要'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 doI resolve it? Could someone explain what's going wrong? (According to the tutorial the test should be passing).

我不知道如何解决这个问题,所以测试通过了。如何我解决这个问题?有人能解释一下出了什么问题吗?(根据教程,测试应该通过)。

回答by Aman Garg

The problem may be that you have not defined default_host for test environment. Define default_host inside config/environments/test.rb like this:

问题可能是您没有为测试环境定义 default_host。在 config/environments/test.rb 中定义 default_host 如下:

config.action_mailer.default_url_options = {:host => "localhost:3000"}

回答by Sheldon

In the end I just used:

最后我只使用了:

root_path

instead of:

代替:

root_url

回答by Prabhakar Undurthi

You've to set the default_url in each environment(development, test, production).

您必须在每个环境(开发、测试、生产)中设置 default_url。

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'

回答by DBrown

For tests like the controller tests shown in the OP's example, another setting is needed to resolve this error. Mentioned by Kesha Antonovyou 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:

对于 OP 示例中显示的控制器测试之类的测试,需要另一个设置来解决此错误。Kesha Antonov提到您可以改用路由的 default_url_options。通过在 config/environments/test.rb 的最后一行(在end 之后)添加以下设置,您的测试将使用给定的主机来构建 URL:

Rails.application.routes.default_url_options[:host] = 'lvh.me:3000'

As mentioned by the OP in another answer, you can instead switch to a PathNamed Routewhen the error is from a URL named route.

正如 OP 在另一个答案中提到的,当错误来自名为 route 的 URL 时,您可以改为切换到Path 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.

如果要为邮件程序预览设置默认主机,则需要其他答案中提到的 action_mailer.default_url_options。

config.action_mailer.default_url_options = { :host => "localhost:3000" }

See Generating URLs in the Action Mailer APIdocs for more information on the action_mailer setting.

有关action_mailer 设置的更多信息,请参阅Action Mailer API文档中的生成 URL 。

回答by 6ft Dan

You can avoid this error by using the skip_confirmation!method.

您可以通过使用skip_confirmation来避免此错误方法。

user = User.new(email: "[email protected]", password: "1234pass5678word")
user.skip_confirmation!
user.save
user