Display range_field value in Rails

Go To StackoverFlow.com

1

I have a range_field that allows a user to select between 0 and 100 in a form. I want to be able to show the value of the range_field, but am lost on how to do it. I am very new to rails and javascript so any help would be appreciated.

Thanks

2012-04-04 01:02
by kavinderd


2

I wouldn't use Javascript, honestly. I use the stock rails range_field in my application. You can check it out here:

range_field

Here is an example code snippet for a range field in my application:

<%= range_field :thing1, :in => 0..100, :step => '.1' %>

This makes a nice little slider that works well on iOS and web and looks okay on Android.

2013-03-09 17:38
by dblarons
This does not show the min / max or current value of the slider... - Gary 2018-05-22 18:17
I wrote this five years ago. I would definitely use Javascript today, ha! @Gary, the default Rails slider is very bare bones - dblarons 2018-05-23 19:47


1

This snippet will let you update the label's value whenever the slider changes (or you could change the .change to .ready if you wanted to display it when the page loaded).

In the form

#form.html.erb
<%= form_for Foobar.new do |f| %>
  <%= f.label :some_size, "Current Value = " %>
  <%= f.range_field :some_size, :in => 0..100, :step => 1 %>
<% end %>

and then the jQuery

#form.js
$("#new_foobar").change(function() { 
  $form = $(this);
  $input_value = $form.children("#foobar_some_size").val();
  $label = $form.children("#foobar_some_size").siblings("label");
  $label.text("Current Value = " + $input_value);
})
2014-06-18 17:07
by teoucsb82
Ads