I'm trying to allow my users to CNAME their custom domain to my Rails app hosted on Heroku. I tried to follow this article but I'm getting this error in my logs when I ran the code:
WARN: NameError: uninitialized constant HerokuDomainJob::Heroku
This is my code in heroku_domain_job.rb
require 'platform-api'
class HerokuDomainJob < ApplicationJob
queue_as :default
def perform(domain, action)
heroku = PlatformAPI.connect_oauth(ENV['HEROKU_API_KEY'])
begin
case action
when "add"
heroku.domain.create(my_app_name, "hostname" => domain)
when "remove"
heroku.domain.delete(my_app_name, domain)
end
rescue Heroku::API::Errors::RequestFailed => e
Rails.logger.error "[Heroku Domain Worker] ERROR: #{e}"
end
end
end
This is the code in my User model
class User < ApplicationRecord
after_save :update_heroku_domains
...
private
def update_heroku_domains
if self.domain_changed? && self.domain_was.present?
HerokuDomainJob.perform_later(self.domain_was, "remove")
end
HerokuDomainJob.perform_later(self.domain, "add")
end
end
Essentially, the code is almost identical as per the article's so I don't really understand where I got it wrong. Appreciate any help.
Thank you.