简体   繁体   English

Ruby on Rails:处理全局常量?

[英]Ruby on Rails: Handling global constants?

I am working on a Ruby on Rails Application with a React Frontend.我正在开发带有 React 前端的 Ruby on Rails 应用程序。

I currently have constants that are needed on both the backend and frontend.我目前有后端和前端都需要的常量。 In order to not have these diverge and to be able to manage them in one place, we store them in our config/initializer files on the backend.为了不让这些分歧并能够在一个地方管理它们,我们将它们存储在后端的config/initializer文件中。 We have a ConstantsController which serves these to the frontend.我们有一个将这些服务提供给前端的ConstantsController

This has been great so far but now, some of the constants are out of date.到目前为止,这很好,但现在,一些常量已经过时了。 Eg for a color constant, we no longer have some colors available and don't want to show them on the frontend.例如对于color常数,我们不再有一些可用的 colors 并且不想在前端显示它们。 However, if we change the constants(removing unused ones) we will break some validations.但是,如果我们更改常量(删除未使用的常量),我们将破坏一些验证。

Right now I can only think of having an Active set of constants that are sent to the frontend which is a subset of the Full set of constants.现在我只能想到有一个Active的常量集被发送到前端,它是Full的常量集的一个子集。 Otherwise I imagine defining the constants in a table with a disabled field and having a Model for each constant.否则,我想在带有禁用字段的表中定义常量,并且每个常量都有一个 Model。 On the frontend we would only display active constants in this case在前端,我们只会在这种情况下显示active常量

I realize that anything that I'm referring to as a constant is not a constant if it is changing.我意识到任何我所说的常数如果它在变化,它就不是常数。 These should instead be handled as settings/types/versions which are stored in tables这些应该作为存储在表中的设置/类型/版本来处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM