Ruby On Rails Updating Heroku Dynamic Routes

不打扰是莪最后的温柔 提交于 2019-12-02 08:20:53

Fixed it!

There is something called an "ActiveRecord Observer" which have been depreciated since Rails 4.0. I found this website which explained what I wanted to do, but it was slightly outdated. I've included by code for rails 4.0 below:

If you're using Rails 4, use the rails-observers gem

Add the call to your environment files:

#config/application.rb (can be placed into dev or prod files if required)
config.active_record.observers = :slug_observer

Add a new observer class into your models folder:

#app/models/slug_observer.rb
class SlugObserver < ActiveRecord::Observer
  def after_save(slug)
    Rails.application.reload_routes!
    slug.logger.info("Routes Reloaded")
  end

  def after_destroy(slug)
    Rails.application.reload_routes!
    slug.logger.info("Routes Reloaded")
  end
end

The way this works is to call these bolt-on functions once the original has been run. This allowed my app to operate the function indepenent of the model in question, thus updating the routes correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!