在我的 Rails 应用程序中,我正在尝试创建一个预订表格,外部各方(parks
)可以指向他们的客户为相应的公园进行预订。预订表格与带有子域书的 url/路由一起使用
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.
概述尝试的方法
Park
有一个website
专栏。在routes.rb
我尝试设置约束并将它们应用于park_availability
动作。- 在保存公园后,
Park
我尝试在模型中将域 ( ) 添加到我的 Heroku 应用程序中。Park.website
- 在我的行动之前,
Park controller
我尝试找到, 。@park
park_availability
代码
路线.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
公园.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