Joe D Joe D - 1 year ago 101
jQuery Question

Get Rails 4 Enum Values Available In Jquery Slider Input

I am preparing an AJAX call and have a few boostrap-slider inputs for variables that correspond to rails Enum variables. I have two questions related this:

  1. I display the slider input on the slider. Right now, it shows the integer that corresponds to the rails enum. I need it to show the value that goes with that integer. Is there a mapping function that I can use in jquery to get that? If so, can I include ERB to get it?

  2. When I submit the AJAX call, should I submit the integer that goes with that enum or the actual value to the server?

Thank you.

Answer Source

The answer to the first question is about finding the exact Rails map and link it to a JS variable. My code to do that required a few other methods but here it is.

var QUALITY = <%= Product::QUALITIES.invert.to_json.html_safe %>

In an ajax call, rails expects the enum rather than the integer. I used the function about to map the integer back to the enum value and submitted that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download