基于导轨中的域设置全局实例变量

发布于 2025-02-12 21:41:38 字数 463 浏览 3 评论 0原文

我有一个单一的铁路应用程序,目前正在通过让我们说 https://www.domain1.com ,我想启用第二个域访问该应用程序,可以说 https://www.domain2.com ,然后将设置全局实例变量。

然后,我想使用此实例变量来控制页面上的某些内容,例如内容,样式和图像。

有什么想法我如何实现这一目标?

I have a single Rails application that is currently being accessed via lets say https://www.domain1.com and I would like to enable the application to be accessed by a second domain, lets say https://www.domain2.com which would then set a global instance variable.

I would then like to use this instance variable to control certain things on the page such as content, styles and images.

Any ideas how I could achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2025-02-19 21:41:38

例如,

def my_action
  @domain_instance_variable = request.domain
  $domain_global_variable = request.domain
end

全局变量和实例变量是不同的。实例变量范围为类的实例,并且全局变量具有全局范围。最小化范围是一个很好的经验法则。

对于控制器,视图或助手的Rails代码,您只需直接调用request.domain而不是将其分配给变量。

E.g.

def my_action
  @domain_instance_variable = request.domain
  $domain_global_variable = request.domain
end

A global variable and an instance variable are different things. An instance variable is scoped to an instance of a class, and a global variable has a global scope. It's a good rule of thumb to minimize scope.

In the case of Rails code in controllers, views or helpers, you can just call request.domain directly instead of assigning that to a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文