Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass updating javascript variable into form to controller in Ruby on Rails?

I have the following javascript:

<script type="text/javascript" charset="utf-8">
$(function() {
    $( "#slider-range" ).slider({
        range: true,
        min: 200,
        max: 2000,
        values: [ 800, 1200 ],
        slide: function( event, ui ) {
            $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
        }
    });

    $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
        " - $" + $( "#slider-range" ).slider( "values", 1 ) );

});

The code creates a price range and a slider bar, price range slides as user moves bar. I want to pass the min price and max price into the form like:

<%= form_tag laptops_path, :method => 'get' do %>
    <%= hidden_field_tag "amount", params[:amount] %>

<%= submit_tag "Search", :name => nil %>

But when I do so, the "amount" is still coming blank when submitted. Any ideas why?

like image 511
Krishna Avatar asked Feb 05 '26 04:02

Krishna


1 Answers

Make sure that your input element has an id attribute (not just name attribue). #amount means nothing without the id attribute (however, you could use: input[name=amount], if that's the problem).

like image 87
Joe Johnson Avatar answered Feb 06 '26 16:02

Joe Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!