Skip to content

Instantly share code, notes, and snippets.

@samiron
Created October 14, 2012 16:42
Show Gist options
  • Save samiron/3889127 to your computer and use it in GitHub Desktop.
Save samiron/3889127 to your computer and use it in GitHub Desktop.
Rails 3: Update fields via ajax
/*
This part of javascript needs to be included to make
a ajax call onchanging the selection value of dropdown.
Here, application.js is shown just for example. You should
load this based on your requirements.
*/
//Use the id of your dropdown instead of "dropdown_id"
$(function($) {
$("#dropdown_id").change(function() {
//The controller method from where you want to get the result
$.ajax({url: '/setval',
data: 'element_id=' + this.value,
dataType: 'script'})
});
});
#This controller method should populate your value
#And assign it in a instance variable.
def setval
@myval = getValueFromSomeLogic(params[:element_id])
end
/*
It will set the value of the text box whose id is
Use the id of your text box instead of "text_box_id"
*/
$('#text_box_id').val('<%= @params %>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment