RuntimeError default_url_options 主机和 root_url(带子域)
我的应用程序的每个用户都有一个存储在 db 中的自定义 url(通过子域)。
我的请求很简单,我想通过 json feed 发送“用户 url”,如下所示:
{user_name: "vincent", :user_url: "vincent.my_app.com"}
为此,我覆盖了用户模型的 as_json 函数:
def as_json(options={})
{
:user_name: self.name,
:user_url: root_url(:subdomain => self.subdomain)
}
end
我有这个附加模块(该模块工作很好,但不是实际问题):
module SubdomainHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
host = ActionMailer::Base.default_url_options[:host]
[subdomain, host].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
但是当我获取 json feed 时,出现此错误:
RuntimeError (Missing host to link to! Please provide :host parameter or set default_url_options[:host]):
指向用户模型的“as_json”方法。
当然,每个环境都在 dev.rb 中实现 default_url_options:
config.action_mailer.default_url_options = {:host => 'my_app:3000'}
我不明白为什么以及如何修复它。请帮我。
Each user of my app have a custom url (through subdomain) stored in db.
My request is simple, I would like to send the "user url" through a json feed, like this:
{user_name: "vincent", :user_url: "vincent.my_app.com"}
to do that I have overwrite the as_json function of user model:
def as_json(options={})
{
:user_name: self.name,
:user_url: root_url(:subdomain => self.subdomain)
}
end
I have this additionnal module (which this work great and isn't the actual problem):
module SubdomainHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
host = ActionMailer::Base.default_url_options[:host]
[subdomain, host].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
But when I get the json feed I have this error:
RuntimeError (Missing host to link to! Please provide :host parameter or set default_url_options[:host]):
pointing to the "as_json" method of user model.
of course each environment implement default_url_options, in dev.rb:
config.action_mailer.default_url_options = {:host => 'my_app:3000'}
I don't understand why and how I fix it. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有类似的问题。不知怎的,它似乎与actionpack的url_helper和我的字段名称有关。似乎是以 *_url 结尾的字段名称存在问题。不完全明白为什么,但我有一个名为 login_url 的字段,我将其重命名为 login_uri (注意从 l 到 i 的更改),它解决了我的问题。
I had a similar problem. Somehow it seemed to be related to actionpack's url_helper and my field name. Seems to be an issue with field names ending in *_url. Don't fully grok why but I had a field named login_url that I renamed to login_uri (notice the change from l to i) and it fixed my problem.
要在模型中使用 url 帮助器
root_path
,我假设您正在混合Rails.application.routes.url_helpers
或类似的东西。这些助手始终构造引用default_url_options
的 URL。ActionController
从当前请求中找出这些默认选项。config.action_mailer.default_url_options
仅适用于没有请求直觉默认值的邮件程序,不适用于模型中。视图委托给控制器/邮件程序。每个混合这些帮助器的类都必须提供default_url_options,但通常重新定义它并不是一个好主意。提供来自控制器、邮件程序或视图的 URL。如果您确实必须这样做,请在您的模型中:
delegate :default_url_options, to: ActionMailer::Base
To use the url helper
root_path
in your model I presume you are mixing inRails.application.routes.url_helpers
or similar. These helpers always construct URLs referencingdefault_url_options
.ActionController
figures out these default options from the current request.config.action_mailer.default_url_options
is only for mailers which have no request to intuit defaults from, not for use in models. Views delegate to the controller/mailer. Each class that mixes these helpers in must providedefault_url_options
, but generally it's just a bad idea to redefine it. Supply the URL from the controller, mailer or view.If you really must, in your model:
delegate :default_url_options, to: ActionMailer::Base