简体   繁体   English

可以从Rails中的控制器获取和设置的应用程序范围的全局变量

[英]Application-wide global variable that can be get and set from controllers in Rails

I'm a new ruby-on-rails developer and I'm stuck building out a function I need. 我是一名新的ruby-on-rails开发人员,但我坚持要构建所需的功能。

I have a product feed on a Rails website. 我在Rails网站上有产品Feed。 I want to make an admin console function where the admin can change the mode of how the product feed will behave for ALL users. 我想创建一个管理控制台功能,管理员可以在其中更改所有用户的产品Feed行为方式。

I need a variable that is application-wide, initialized at the start of the server, but can be toggled when ever the controller button is selected. 我需要一个在应用程序范围内的变量,该变量在服务器启动时初始化,但是可以在选择控制器按钮时进行切换。

I tried implementing a cache solution, setting variables in application.rb, and a few other techniques but haven't managed to get anything working. 我尝试实现缓存解决方案,在application.rb中设置变量,以及其他一些技术,但没有设法使任何工作正常。

Looking for some insight into how to make a writable, global variable for a rails site! 寻找有关如何为Rails网站创建可写的全局变量的见解!

Thank you 谢谢

Since you are working stateless, but you want to this "global" to persist across all your Rails's servers, you will need to go do the DB for the persistence: 由于您正在无状态工作,但是您希望此“全局”在所有Rails的服务器上持久存在,因此您需要执行DB来实现持久性:

class ProductFeed < ActiveRecord::Base
  validates_presence_of :toggle
end

class ApplicationController < ActionController::Base
  before_filter -> {
    @product_feed = ProductFeed.last
  }
end

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

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