RuntimeError default_url_options 主机和 root_url(带子域)

发布于 2024-10-31 08:21:44 字数 1208 浏览 1 评论 0原文

我的应用程序的每个用户都有一个存储在 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 技术交流群。

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

发布评论

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

评论(2

兔姬 2024-11-07 08:21:44

我有类似的问题。不知怎的,它似乎与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.

唠甜嗑 2024-11-07 08:21:44

要在模型中使用 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 in Rails.application.routes.url_helpers or similar. These helpers always construct URLs referencing default_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 provide default_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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文