Your view code should only be:
<div id="slider"></div> <%= f.hidden_field :my_score %>
You already have a hidden field, you donβt need a second one. There is also no need for a value, the rails will put one if it already exists (i.e. when editing).
As for your javascript, you will have a change event as follows:
change: function(event, ui) { $('input#model_name_my_score').val(ui.value); }
I do not know the exact identifier that your hidden field will have, but usually it is model_name_name_file_name. You will need to check this out.
The last thing you will need to do is set the slider to the desired value when loading the page (provided that there is a version for editing your form and the existing value may be there).
$( "#slider" ).slider({ value: $('input#model_name_my_score').val(),
If you want 100 to be the default when creating a new object, then you must set 100 in the model or when you want to set the default values, so the rails will pre-fill the hidden field with this value on the "new" form. You can check the gem, for example https://github.com/FooBarWidget/default_value_for , if you do not already have a way to handle the default values.
source share