69

现在我正在使用它,它适用于开发主机,但是当我转移到生产环境时,我必须手动更改 {:host => ""} 代码。

post.rb

def share_all
  url =  Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000')
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

我想使用它,然后为每个环境定义 default_url_options:

post.rb

def share_all
  url =  Rails.application.routes.url_helpers.post_url(self)
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

我尝试将其添加到我的 config/environments/development.rb 但我仍然收到“缺少要链接的主机!请提供:host 参数或设置 default_url_options[:host]”错误

发展.rb

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

我什至这样尝试过:

发展.rb

config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}

编辑:

我现在也遵循了这个并且仍然是相同的错误指南http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

应用控制器

class ApplicationController < ActionController::Base
  protect_from_forgery
  include ApplicationHelper
  def default_url_options
    if Rails.env.production?
      { :host => "example.com"}
    else
      {:host => "example1.com"}
    end
  end
end

这让我发疯,我在这里想念什么???

4

8 回答 8

127

好的,我想出了正确的写法是

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

:)

于 2011-04-20T04:02:43.027 回答
12

default_url_options从.继承您的应用程序ActionMailer

您希望尽可能保持 DRY,因此理想情况下,您不希望在同一环境的多个位置对主机和端口进行硬编码,除非您ActionMailer 实际使用的主机和端口与Application.

default_url_options为整个设置Application,只需将以下行添加到您的config/environment.rb文件中(更改MyApp为您的应用程序的名称):

# 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

这将解决您的问题并自动将您Application的 '设置default_url_options为与您的相同config.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"}
于 2018-01-30T20:40:03.200 回答
6

在对此文件的更改生效之前,您必须重新启动服务器。

于 2011-04-17T05:43:20.807 回答
4

config.action_mailer.default_url_options = { :host => "你的主机" }

例如您的主机 localhost:3000

您可以将其放在 test.rb、development.rb、production.rb 文件中,主机可能因环境而异

于 2018-07-18T16:36:08.143 回答
3

config/environments/development.rb (任何其他环境,相同)

将此行添加到您想要的主机

routes.default_url_options[:host] = 'localhost:3000'
于 2020-09-30T19:19:11.053 回答
2

我知道这是一个旧线程,但我在 Ruby 2.6.3 和 Rails 5.2.3 中遇到了这个问题。我看到的行为基本上是我添加的每条路径都会失败Error during failsafe response: undefined method 'empty?' for nil:NilClass。在生产中它运行良好,但在我的开发环境中,我会收到上面提到的错误。


对我的修复是将其添加到controllers/application_controller.rb

def default_url_options
  if Rails.env.production?
    Rails.application.routes.default_url_options = { host: "www.production-domain.com", protocol: 'https' }
  elsif Rails.env.development?
    Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'http' }
  end
end

然后我就可以在本地运行我的开发环境了。

于 2019-10-08T23:58:51.757 回答
0

对我来说有效的是

ActionMailer::Base.default_url_options = { host: "yourhosthere"} # e.g. yourhosthere=localhost:3000 or yourhosthere=example.com

因为如果您已经在配置文件中设置了端口,那么仅更改 [:host] 将导致错误

TypeError (no implicit conversion of Symbol into Integer):
于 2020-08-09T11:15:08.567 回答
0

我自己尝试在 rake 任务中生成 URL 时遇到了这个问题。这绝对不是显而易见的,其中许多解决方案都可以工作。我的解决方案是从@joshua-pinter's 起飞,但在环境文件中完成。例如我的development.rb样子:

Rails.application.configure do
  …
  config.action_mailer.default_url_options = self.default_url_options = { host: 'localhost', port: 3000 }
  …
end

production.rb.

于 2021-07-11T18:31:52.390 回答