Deepank Vora Deepank Vora - 1 year ago 129
Ajax Question

Rails select_tag - how to send ajax request?

I want to send an AJAX request once an option is selected (changed) from the

in Rails 3.1. Can we use
:onchange => remote_function()
or is there some other way ?

Answer Source

You should definitely do this with jQuery (with an unobtrusive approach to keep things as clean as possible). On your .js file you should have something like this...

    url: "remote_action_in_your_controller",
    type: "GET",
    data: {select_tag_value: $('#select_tag_id option:selected').text()},

this is as simple as it should be if you just want to call a remote action on your controller (notice I passed the value of the selected option since you probably want that value to do something with it). If you need to do something after the ajax call is executed use the succes option. Hope this helps.