Ruby On Rails更新Heroku动态路由 [英] Ruby On Rails Updating Heroku Dynamic Routes

查看:106
本文介绍了Ruby On Rails更新Heroku动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用应用程序范围内的slu((使用说这是在Heroku上运行多个进程的问题,但我还没有找到。

有什么想法?非常感谢提前

解决方案

修正了它!



称为 ActiveRecord Observer ,自从Rails 4.0以来这些已经被折旧了。我找到了这个网站,它解释了我想做,但它有点过时。我在下面的代码中包含了rails 4.0:



如果您使用的是Rails 4,请使用 rails-observers gem



将呼叫添加到您的环境文件:

 #config / application.rb(如果需要,可以放在dev或prod文件中)
config.active_record.observers =:slug_observer

在模型文件夹中添加一个新的观察者类

 #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)
结束
结束

这种方法的作用是在原件运行后调用这些螺栓固定功能。这使我的应用程序能够操作相关模型的独立功能,从而正确更新路线。


I've got an app which uses app-wide slugs (uses Slugalicious gem with Sluggable table), and have routed to these slugs by using this code:

  #Slugs
  begin  
      Slug.all.each do |s|
        begin
          get "#{s.slug}" => "#{s.sluggable_type.downcase.pluralize}#show", :id => s.slug
        rescue
        end
      end
  rescue
  end

I currently update the routes when I update the slugs model with this code:

after_save :update_routes

def update_routes
    Rails.application.reload_routes!
end

This works perfectly in dev:

The problem I'm getting is that if I update or create a new slug, Heroku doesn't update to accommodate it. It works in development, and (confusingly), it works if I do "heroku run rake routes" on Heroku. But the app just shows a 404 if I try to browse to the new URL. If I redeploy the app, all the routes work, which leads me to believe it's something to do with updating the routes app-wide

This question said it's something to do with running multiple processes on Heroku, but I've yet to find this.

Any ideas? Many thanks in advance

解决方案

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.

这篇关于Ruby On Rails更新Heroku动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆