Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check to see if a Rails.cache key exists using jQuery

I'm trying to replicate the following Rails method, but using JQuery:

Rails.cache.exist?("ped_basic")

Does anyone know how to do this?

like image 896
2scottish Avatar asked Oct 19 '25 14:10

2scottish


1 Answers

I am still experimenting with the optimal way to do this, but I've ended up using the 'Gon' gem to create a variable in my controller method (before_filter) that is able to be referenced by Rails as well as by jQuery.

In my controller method, I set my cache key equal to a gon.variable value, then can access the variable in jQuery via the same gon.variable name.

In other words:

#Controller method
before_filter :set_gon_vars

def set_gon_vars
  gon.variable1 = Rails.cache.exist?("cache1")
  gon.variable2 = Rails.cache.exist?("cache2")
  gon.variable3 = Rails.cache.exist?("cache3")
end

The the variables can be accessed in my coffeescript code by simply referencing the same gon.variable name.

like image 62
2scottish Avatar answered Oct 21 '25 05:10

2scottish