Rails全局变量未在更新时更新(Rails global variables are not updating on update)

编程入门 行业动态 更新时间:2024-10-28 06:35:01
Rails全局变量未在更新时更新(Rails global variables are not updating on update)

我在application_controller.rb中定义了一个$template变量,其中包含一些通过admin更改的站点模板代码

问题是当我保存或更新它时,它不会影响站点直到服务器重启,这在生产中真的很不舒服

$template = Template.first ..... $template.header.html_safe

在日志中,我可以使用那个$ template变量make和SQL以及最开始(在Puma启动之前),这可能是全局变量应该如何工作的方式。

PS我使用active_admin并从那里更新

I have an $template variable defined in application_controller.rb which contains some site template code changed though admin

The problem is when I save or update it, it doesn't affect site till server restart, which is really uncomfortable in production

$template = Template.first ..... $template.header.html_safe

In logs I can the that $template variable make and SQL and the very beginning (before Puma booted) and probably that's the way how global vars should be working.

P.S. Im using active_admin and update from there

最满意答案

这是因为你获取你的Template并且它在内存中,因此在每次save (:create,:update,create!,:update!,:save,:save!)之后你必须重新加载你的变量

$template.reload

这将更新您的变量

或者你可以只在你的ApplicationController添加辅助方法

def first_template Template.first end helper_method :first_template

这将每次检索更新的数据

你可以在你的视图中使用它

first_template.header.html_safe

it is because you fetch your Template and it is in memory, so after each save (:create, :update, create!, :update!, :save, :save!) you have to reload your variable

$template.reload

this will update your variable

or you can just add helper method to your ApplicationController

def first_template Template.first end helper_method :first_template

this will retrieve updated data each time

and you can use it in your view

first_template.header.html_safe

更多推荐

本文发布于:2023-08-06 07:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局变量   global   Rails   update   updating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!