简体   繁体   中英

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.

I have a product feed on a Rails website. 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.

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.

Looking for some insight into how to make a writable, global variable for a rails site!

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:

class ProductFeed < ActiveRecord::Base
  validates_presence_of :toggle
end

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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