在Rails应用程序中管理多个域:“ NameError(未初始化的常量#<0x0000563d7bf62250>:: Heroku):”0x0000563d7bf62250>

问题描述 投票:0回答:1

[在我的Rails应用程序中,我正在尝试创建一个预订表单,外部各方(parks)可以指示其客户为各个公园进行预订。预订表单适用于子域book

的网址/路线
https://book.myapp.com/en/parks/:park_id/park_availability

目标

我想用myapp.com的网站替换我的域(park),以便得到

https://book.parkapp.com/en/park_availability

不幸的是我收到错误消息创建公园后

NameError (uninitialized constant #<Class:0x0000563d7bf62250>::Heroku):

使用现有公园时

{park.website}'s server IP address could not be found.

尝试使用概述的方法

  1. Park具有website列。在routes.rb中,我尝试设置约束并将其应用于park_availability操作。
  2. 在保存公园之后,在Park模型中,我试图将域(Park.website)添加到我的Heroku应用程序中。
  3. Park controller动作之前,我尝试在@park中找到park_availability

代码

routes.rb

class CustomDomainConstraint
  # Implement the .matches? method and pass in the request object
  def self.matches? request
    matching_site?(request)
  end

  def self.matching_site? request
    # handle the case of the user's domain being either www. or a root domain with one query
    if request.subdomain == 'www'
      req = request.host[4..-1]
    else
      req = request.host
    end

    # first test if there exists a Site with a domain which matches the request,
    # if not, check the subdomain. If none are found, the the 'match' will not match anything
    Park.where(:website => req).any?
  end
end

Rails.application.routes.draw do
  resources :parks do
     match ':website/park_availability' =>  'parks#park_availability', on: :member, :constraints => CustomDomainConstraint, via: :all
end
end

park.rb

class Park < ApplicationRecord
  after_save do |park|
  heroku_environments = %w(production staging)
  if park.website && (heroku_environments.include? Rails.env)
    added = false
    heroku = Heroku::API.new(api_key: ENV['HEROKU_API_KEY'])
    heroku.get_domains(ENV['APP_NAME']).data[:body].each do |domain|
      added = true if domain['domain'] == park.website
    end

    unless added
      heroku.post_domain(ENV['APP_NAME'], park.website)
      heroku.post_domain(ENV['APP_NAME'], "www.#{park.website}")
    end
  end
end

parks_controller.rb

class ParksController < ApplicationController
before_action :find_park, only:[:park_availability]

def park_availability
  #working code...
end

private
def find_park
     # generalise away the potential www. or root variants of the domain name
    if request.subdomain == 'www'
      req = request.host[4..-1]
    else
      req = request.host
    end

    # test if there exists a Park with the requested domain,
    @park = Park.find_by(website: req)

    # if a matching site wasn't found, redirect the user to the www.<website>
    redirect_to :back
  end

end
ruby-on-rails heroku cross-domain heroku-api
1个回答
0
投票

要解决有问题的错误,您可以尝试将类名写为::Heroku,这将使ruby在正确的范围内查找它。

但是Heroku legacy api已经是disabled,因此您应该使用其new platform-api

heroku = PlatformAPI.connect(ENV['HEROKU_API_KEY']) # note that you may also have to use a new key
domains = heroku.domain.list(ENV['APP_NAME'])
...
heroku.domain.create(ENV['APP_NAME'], ...)

也可以检查域是否存在,您可以使用domain.info api方法,而不是获取不需要的所有域。

请记住,回调不是进行任何外部调用的最佳位置:如果api调用由于某种原因而失败(临时网络问题,api中断,服务器重启等)-整个事务将被回滚,项目不会保存,您可以松散数据。更好的方法是在此排队一个后台作业,该作业将在以后处理域,并在需要时可以重试,等等。

© www.soinside.com 2019 - 2024. All rights reserved.