在我的应用程序中,我有一个custom_page模型,就像在任何其他cms中一样。管理员/主持人可以使用文本编辑器创建自定义页面编辑内容并指定slug 在自定义页面模型中,我有:
after_create do
Rails.application.reload_routes!
end
在路线中我有这个:
CustomPage.where.not(slug: nil).all.each do |page|
get "/#{page.slug}", controller: "custom_pages", action: "show", id: page.id
end
它在开发环境中工作得很好,但正如你可能期望在使用puma生产时,只有一个进程/线程(我不知道哪个/如何)重新加载路由。你们有什么想法我怎么能在所有过程中重新加载路线?提前谢谢
答案 0 :(得分:0)
我使用通配符来修复此问题(无论如何重新加载路线都不是一个漂亮的解决方案),所以我的路线现在看起来像这样:
get "/*slug", to: "custom_pages#show"
并在我的custom_page控制器中:
before_action :get_page
def get_page
@page = CustomPage.friendly.find_by_slug(params[:slug])
end
这好多了