在初始化时设置了default_url_options
问题描述:
我需要在我的Rails应用程序的其中一个环境中强制主机。在初始化时设置了default_url_options
我已经可以得到覆盖由包括
def default_url_options(opts={})
opts.merge({:host => 'stg.my-host.com'})
end
在app /控制器/ application.rb中
工作,但有没有办法来设置这个初始化上,最好是在配置/环境/ ...文件?我想保持有条件的env逻辑出来的控制器。
但是当我尝试
config.action_controller.default_url_options = { ... }
甚至
ActionController::Base.default_url_options = { ... }
我得到 “未定义的方法,” 即使在config.after_initialize一个包裹{...}
任何想法?
答
答案是......这是不可能的,因为default_url_options是作为函数实现的,而不是attr。
从action_pack/action_controller/base.rb:1053:
# Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in
# the form of a hash, just like the one you would use for url_for directly. Example:
#
# def default_url_options(options)
# { :project => @project.active? ? @project.url_name : "unknown" }
# end
#
# As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic decisions about the
# urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set
# by this method.
def default_url_options(options = nil)
end
答
您可以强制配置是这样的:
config.action_mailer.default_url_options = { :host => "foo.com" }
在你的代码的问题是,你使用config.action_controller
代替config.action_mailer
这是因为他不需要设置action_mailer选项,所以他没有犯错,我想为不同的问题提供一个答案。 – 2013-09-13 03:43:18