[英]Subdomain in ActiveModel::Serializer
我用来生成JSON ActiveModel :: Serializer(gem'active_model_serializers')。 我有一个子网域。 有帮助的。
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain, request.port_string].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
如何使用子域返回整个URL?
class TestSerializer < ActiveModel::Serializer
attributes :id, :url
def url
object.url # get "testurl"
# root_path(subdomain: object.url) # does not work
end
end
因此它返回“ testurl”-http: http://example.com/testurl
必须返回http://testurl.example.com/
如评论所示, root_path
必须更改为root_url
。 _path
辅助程序不会产生主机名,因此使用它们的输出生成的链接将指向当前域/子域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.