How to cache / speed up dropdown data in Ruby on Rails

I have a problem with a page that takes a long time to update a drop down list. Basically, when changing one drop-down value in the form, the potential options for the second drop-down list change to another list.

How can I speed this up? I am using REDIS for other reasons if this can help.

Thank!

+3
source share
3 answers

If you have several parameters in the main choice, then you can set all possible additional parameters in the js hash array the first time you render the page, and then change the primary search from this js array on the client side - this is obviously quick, but may slow down the loading of the start page or inflate the page size if there are too many options.

Another way will still make an ajax call when changing the primary selection, but it caches the action that returns the secondary parameters. This will allow him to cache based on the parameters that form the request and use the result in cache rails whenever possible (instead of always pressing redis or db).

+2
source

An excellent answer from Andrei Kuklevich. To add to it ...

Rails " .

, example.html.erb:

<% cache do %>
  <script>
    var javascript_hash = {name: '<%=@ruby_val%>'};
  </script>
<% end %>

ajax, example.js.erb:

<% cache do %>
  alert('hello from javascript');
<% end %>

, , ActiveRecord:: Relation :

Company.where({name: 'foo'}) # this
Company.where({name: 'foo'}).all # not this
+2

, JSON JavaScript, , .

- , /cache/drop_down.state1.js .., . Rails /public, JavaScript, . .

You can do this for each drop-down list independently or, if it’s practically possible, all the drop-down lists at the same time, and then turn it on and select the corresponding key from the list.

This requires the creation of a client side script to dynamically create and populate a secondary drop-down field, including selecting the appropriate record if you need to pre-select it. However, this is not so difficult with a helper library such as jQuery.

+1
source

All Articles