Search code examples
jqueryruby-on-railsajaxenumsbootstrap-slider

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.


Solution

  • 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.