gpt4 book ai didi

ruby-on-rails - 包括 Rails.application.routes.url_helpers 导致 ArgumentError Missing host 链接到

转载 作者:行者123 更新时间:2023-12-04 05:38:15 25 4
gpt4 key购买 nike

我正在开发 Rails 3.1.1。
在模型中包含 url_helpers 会导致保存模型时出现 ArgumentError。

class Medium < ActiveRecord::Base
include Rails.application.routes.url_helpers

.
.
end

class MediaController < ApplicationController

def create
@medium = Medium.new(params[:medium])
@media.save # => cause ArgumentError
end
end

ArgumentError(缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true):

另一个包含 url_helper 的模型不会导致错误。
怎么了?
提前致谢。

最佳答案

您需要在使用帮助程序的调用中将主机作为参数传递:
Rails.application.routes.url_helpers.media_url(:host => "localhost:3000")
或像这样的不同路线:
Rails.application.routes.url_helpers.media_url(self, :host => AppConfig.host)
其中 AppConfig.host 是取决于环境的主机(localhost:300 用于开发环境)。

关于ruby-on-rails - 包括 Rails.application.routes.url_helpers 导致 ArgumentError Missing host 链接到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com