Rails:缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]

发布于 2024-12-01 15:54:09 字数 564 浏览 1 评论 0原文

我已经在谷歌上搜索了大约 90 分钟,但仍然没有答案。在哪里设置 default_url_options?我已经将其设置为 config.action_mailer.default_url_options 来解决其他地方的相同错误,但现在在尝试在 RSpec 规范中使用 URL 帮助程序时遇到此错误。我不知道在哪里设置 default_url_options 。

 Failure/Error: listing_url(listing).should match(/\/\d+-\w+$/)
 RuntimeError:
   Missing host to link to! Please provide :host parameter or set default_url_options[:host]
 # ./spec/routing/listing_routing_spec.rb:9:in `block (3 levels) in <top (required)>'

这段代码与 emails/ActionMailer 无关,它只是需要一个 URL 而不是路径。

有什么想法吗?

I have been googling for about 90 minutes now and still don't have an answer to this. Where do I set default_url_options? I've already set it for config.action_mailer.default_url_options to solve this same bug elsewhere, but now I'm getting this error when trying to use a URL helper inside an RSpec spec. I have no idea where it's expecting default_url_options to be set.

 Failure/Error: listing_url(listing).should match(/\/\d+-\w+$/)
 RuntimeError:
   Missing host to link to! Please provide :host parameter or set default_url_options[:host]
 # ./spec/routing/listing_routing_spec.rb:9:in `block (3 levels) in <top (required)>'

This code has nothing to do with emails/ActionMailer, it just happens to need a URL instead of a path.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

傲鸠 2024-12-08 15:54:09

您需要在每个环境中添加以下行:

config.action_mailer.default_url_options = { :host =>; "yourhost" }

这样,它就可以在所有环境中工作,并且可能因环境而异。例如:

开发.rb

config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }

测试.rb

config.action_mailer.default_url_options = { :host => "test.yourhost.com" }

生产.rb

config.action_mailer.default_url_options = { :host => "www.yourhost.com" }

You need to add the following line at every environment:

config.action_mailer.default_url_options = { :host => "yourhost" }

That way, it can work in all environments and could be different from environment to environment. For example:

development.rb

config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }

test.rb

config.action_mailer.default_url_options = { :host => "test.yourhost.com" }

production.rb

config.action_mailer.default_url_options = { :host => "www.yourhost.com" }
哆兒滾 2024-12-08 15:54:09
Your::Application.routes.draw do
  default_url_options :host => "example.com"

  # ... snip ...
end

routes.rb中的某个地方:)

Your::Application.routes.draw do
  default_url_options :host => "example.com"

  # ... snip ...
end

Somewhere in routes.rb :)

虐人心 2024-12-08 15:54:09

应在每个环境的配置文件中指定主机。例如:

config/environments/development.rb

参见这个问题这个问题

The host should be specified in each environment's config file. Eg:

config/environments/development.rb

See this question and this question.

淡莣 2024-12-08 15:54:09

设置 default_url_options 以使用您的 action_mailer.default_url_options

在每个环境文件(例如 development.rbdevelopment.rb 等)中,您可以指定用于 default_url_options >action_mailer:

config.action_mailer.default_url_options = { host: 'lvh.me', port: '3000' }

但是,这些未针对 MyApp:Application.default_url_options 设置:

$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

$ MyApp::Application.default_url_options
#=> {}

这就是为什么您在 ActionMailer 之外的任何内容中都会收到该错误。

您可以将应用程序的 default_url_options 设置为使用您在适当的环境文件(development.rbdevelopment.rb)中为 action_mailer 定义的内容 等)。

为了尽可能保持干燥,请在您的 config/environment.rb 文件中执行此操作,这样您只需执行一次:

# Initialize the rails application
MyApp::Application.initialize!

# Set the default host and port to be the same as Action Mailer.
MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options

现在,当您启动应用程序时,整个应用程序的 default_url_options< /code> 将匹配您的 action_mailer.default_url_options

$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

@pduersteler 带领我走上这条路。

Set default_url_options to use your action_mailer.default_url_options.

In each of your environment files (e.g. development.rb, production.rb, etc.) you can specify the default_url_options to use for action_mailer:

config.action_mailer.default_url_options = { host: 'lvh.me', port: '3000' }

However, these are not set for MyApp:Application.default_url_options:

$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

$ MyApp::Application.default_url_options
#=> {}

That's why you are getting that error in anything outside of ActionMailer.

You can set your Application's default_url_options to use what you defined for action_mailer in the appropriate environment file (development.rb, production.rb, etc.).

To keep things as DRY as possible, do this in your config/environment.rb file so you only have to do this once:

# Initialize the rails application
MyApp::Application.initialize!

# Set the default host and port to be the same as Action Mailer.
MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options

Now when you boot up your app, your entire Application's default_url_options will match your action_mailer.default_url_options:

$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

Hat tip to @pduersteler for leading me down this path.

后eg是否自 2024-12-08 15:54:09

当您使用任何 listing_url 方法时,将返回完整的 URL(而不是通常的相对 URL)。这就是为什么 Rails 要求您提供主机来计算整个 URL。

你如何告诉rails主机?您可以通过多种方式完成此操作:

1.将此选项添加到每个环境:

[/config/development.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/test.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/production.rb]
config.action_mailer.default_url_options = { host: "www.example.com" }

注意:如果您在 rails 引擎中工作,请记住在引擎测试中对虚拟应用程序执行相同的操作:path_to_your_engine/test/dummy/config/environments/* 因为当您测试引擎时,rails 正在测试引擎。

2.将主机选项添加到 foo_url 方法,如下所示:

listing_url(listing, host: request.host) # => 'http://localhost:3000/listings/1'

3.不使用选项 :only_path to true 输出主机。

listing_url(listing, only_path: true ) # => '/listings/1'   

恕我直言,我没有看到这一点,因为在这种情况下我将使用 listing_path 方法

When you use any listing_url method the full URL will be returned(not a relative one as normal). That's why rails is asking you for the host, to compute the whole URL.

How you can tell rails the host? You can do it in several ways:

1.Adding this option to each environment:

[/config/development.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/test.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/production.rb]
config.action_mailer.default_url_options = { host: "www.example.com" }

NOTE: If you are working inside a rails engine remember to do the same for your dummy app inside the engine tests: path_to_your_engine/test/dummy/config/environments/* because when you test the engine it's what rails is testing against.

2.Add the host option to the foo_url method like this:

listing_url(listing, host: request.host) # => 'http://localhost:3000/listings/1'

3.Not output the host with the option :only_path to true.

listing_url(listing, only_path: true ) # => '/listings/1'   

IMHO I don't see the point on this one because in this case I would use the listing_path method

不语却知心 2024-12-08 15:54:09

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

在developemnt.rb / test.rb中,可以更简洁如下:

Rails.application.configure do
  # ... other config ...

  routes.default_url_options[:host] = 'localhost:3000'
end

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

In the developemnt.rb / test.rb, can be more concise as following:

Rails.application.configure do
  # ... other config ...

  routes.default_url_options[:host] = 'localhost:3000'
end
£冰雨忧蓝° 2024-12-08 15:54:09

有趣的是,设置 config.action_mailer.default_url_options 对我没有帮助。另外,在我觉得不属于的地方乱搞与环境无关的设置对我来说并不令人满意。此外,我想要一个在 sidekiq/resque 工作人员中生成 url 时有效的解决方案。

到目前为止,我的方法进入 config/environments/{development, production}.rb:

MyApp::Application.configure do
    # Stuff omitted...

    config.action_mailer.default_url_options = {
      # Set things here as usual
    }
end

MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options

这对我来说在 Rails >= 3.2.x 中有效。

Funny thing, that setting config.action_mailer.default_url_options does not help for me. Also, messing around with environment-independent settings in places I felt like it does not belong was not satisfying for me. Additionally, I wanted a solution that worked when generating urls in sidekiq/resque workers.

My approach so far, which goes into config/environments/{development, production}.rb:

MyApp::Application.configure do
    # Stuff omitted...

    config.action_mailer.default_url_options = {
      # Set things here as usual
    }
end

MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options

This works for me in rails >= 3.2.x.

慢慢从新开始 2024-12-08 15:54:09

您始终可以将主机作为参数传递给 URL 帮助器:

listing_url(listing, host: request.host)

You can always pass host as a parameter to the URL helper:

listing_url(listing, host: request.host)
不必在意 2024-12-08 15:54:09

上面的答案对我不起作用,至少不是我想要的。
我意识到
config.action_mailer.default_url_options = { 主机:“localhost”,端口:3000 }
安装设备后。
希望它能帮助遇到同样问题的人。

The above answer did not work for me, at least not as I wanted.
I realised
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
after installing devise.
Hope it will help someone with the same problem.

骄兵必败 2024-12-08 15:54:09

转到 config/environments/test.rb

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

go to config/environments/test.rb

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

嘴硬脾气大 2024-12-08 15:54:09

以防万一有人发现此搜索有关 ActiveStorage 的错误:

如果您有一个控制器操作,想要使用本地光盘服务(最有可能在测试环境中)生成上传 URL 等,则需要包含 ActiveStorage ::SetCurrent 在控制器中,以便让 blob.service_url_for_direct_upload 正常工作。

just in case someone finds this searching for errors concerning ActiveStorage:

if you have a controller-action where you want to generate upload-urls etc with the local disc-service (most likely in test environment), you need to include ActiveStorage::SetCurrent in the controller in order to allow blob.service_url_for_direct_upload to work correctly.

澜川若宁 2024-12-08 15:54:09

您可以在应用程序控制器中设置默认 url 选项:

class ApplicationController < ActionController::Base
  def default_url_options
    {:locale => I18n.locale}
  end
end

http://guides.rubyonrails.org/action_controller_overview。 html#default_url_options

You can set default url options in the Application Controller:

class ApplicationController < ActionController::Base
  def default_url_options
    {:locale => I18n.locale}
  end
end

http://guides.rubyonrails.org/action_controller_overview.html#default_url_options

〆一缕阳光ご 2024-12-08 15:54:09

我也有同样的错误。我已经正确编写了所有内容,包括教程中的清单 10.13。

Rails.application.configure do
.
.
.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delevery_method :test
host = 'example.com'
config.action_mailer.default_url_options = { host: host }
.
.
.
end

显然用我的服务器网址替换了“example.com”。

我在教程中掩盖的是这一行:

重新启动开发服务器以激活配置后...

所以我的答案是关闭服务器然后再次打开。

I had this same error. I had everything written in correctly, including the Listing 10.13 from the tutorial.

Rails.application.configure do
.
.
.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delevery_method :test
host = 'example.com'
config.action_mailer.default_url_options = { host: host }
.
.
.
end

obviously with "example.com" replaced with my server url.

What I had glossed over in the tutorial was this line:

After restarting the development server to activate the configuration...

So the answer for me was to turn the server off and back on again.

醉态萌生 2024-12-08 15:54:09

在路由中添加 default_url 不是正确的解决方案,但它适用于某些情况。

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

你需要做出这些改变。

    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'

Adding the default_url in routes not the right solution although, it works for some cases.

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'
世态炎凉 2024-12-08 15:54:09

解决了这个问题。

我通过将environment.rb配置为YourApp::Application.default_url_options = YourApp::Application.config.action_mailer.default_url_options

您需要针对每个环境(如开发、测试、登台和生产等)为操作邮件程序设置default_url_options。

参考: <一href="https://stackoverflow.com/questions/7219732/missing-host-to-link-to-please-provide-host-parameter-or-set-default-url-optio/48529627#48529627">缺少主机链接到!请提供 :host 参数或设置default_url_options[:host]

I solved the issue by configuring environment.rb as

YourApp::Application.default_url_options = YourApp::Application.config.action_mailer.default_url_options

You need to set default_url_options for action mailer against each environment like development, testing, staging and production etc.

Reference: Missing host to link to! Please provide :host parameter or set default_url_options[:host]

谁人与我共长歌 2024-12-08 15:54:09

不想改变其他环境的行为,所以我使用:

development.rb

Rails.application.configure do
...
end

Rails.application.default_url_options = Rails.application.config.action_mailer.default_url_options

Works in Rails 6。

Didn't want to change the behavior for other environments, so I used:

development.rb

Rails.application.configure do
...
end

Rails.application.default_url_options = Rails.application.config.action_mailer.default_url_options

Works in Rails 6.

一腔孤↑勇 2024-12-08 15:54:09

Rails.application.config.action_mailer.default_url_options

config.action_mailer.default_url_options = { host: 'my.domain', protocol: 'https', port: 356 }

Rails.application.default_url_options

self.default_url_options = config.action_mailer.default_url_options

首先设置 Action Mailer 默认 url 选项(如大多数答案中所示)

,然后分配该选项到应用程序。最初的问题正是关于这个配置的!与邮件程序无关:)奇怪的是,问题很旧,但并非所有答案都设置此配置,并且没有提到简单的 self 分配

将这些行放入 config/application.rb 或进入特定环境,例如进入 config/environments/development.rb

之后,您可以像这样访问路由助手

Rails.application.routes.url_helpers.user_url(25)
# => "https://my.domain:356/users/25"

Rails.application.config.action_mailer.default_url_options

config.action_mailer.default_url_options = { host: 'my.domain', protocol: 'https', port: 356 }

Rails.application.default_url_options

self.default_url_options = config.action_mailer.default_url_options

Firstly setup Action Mailer default url options (like in the most of the answers)

And after that assign that options to Application. Initial question is exactly about this config! Not about the mailer:) It is strange that question is old but not all answers setup this config and didn't mention simple self assignment

Put such lines into config/application.rb or into specific environment, for example into config/environments/development.rb

After that you can access to routes helpers like this

Rails.application.routes.url_helpers.user_url(25)
# => "https://my.domain:356/users/25"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文