ruby - How do I declare a variable that is accessible throughout the whole Rails application? -
my application depends upon variable page_name value in variable page_name set after submitting form.
should use session? when searched found out sessions destroyed if application behind load-balancer.
what better way make variable accessible throughout whole rails application?
creating constant have solved problem value in variable not set until user fill data in form field , submits data. basically, user responsible fill value in variable whole application depends upon.
you'll need persist value in either database (using activerecord), or in shared memcache server (assuming can accept variable lost if pushed out of cache).
you'll set database value example in controller create action other model.
first, create model
rails generate model variable_name variable_value to save value in def create action:
@app_store = appstore.find_or_initialize_by_name('variable_name') @app_store.variable_value = secure_params[:variable_value] @app_store.save then retrieve anywhere in app with
@app_store = appstore.find_by_name('variable_name') val = @app_store.variable_value
Comments
Post a Comment