Created
October 14, 2012 16:42
-
-
Save samiron/3889127 to your computer and use it in GitHub Desktop.
Rails 3: Update fields via ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This controller method should populate your value | |
#And assign it in a instance variable. | |
def setval | |
@myval = getValueFromSomeLogic(params[:element_id]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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