rails function called from jquery
looking for a way to implement a call to a rails method from within jquery, but without rendering anything, just in the background.
The idea is that I have a select tag with multiselect enabled, my model has a boolean field called active, and when an element is selected, active would be set to true, otherwise false.
in my application.js i have this:
$(".multiselect").change(function() {
$("option", this).each(function() {
if(this.selected) {
// do true for checked
} else {
// do false for unchecked
}
}
});
this function works fine, but i'm stuck at two parts, i know i can get the id of the object from (this).val(), but where should I put the function that would find it and change the active fi开发者_JS百科eld? In the controller or in the model? Mind I don't need to render anything, just do things in the background.
And secondly, how do i call this function from within my jquery?
Any help is greatly appreciated!
You could submit the form every time the change event gets triggered. This would get mapped to a function in the controller, which will render nothing.
You could do something like this
in your application.js
$(".multiselect").change(function() {
$("option", this).each(function() {
if(this.selected) {
// do true for checked
} else {
// do false for unchecked
}
}
//submitting form after doing stuff
$.post($(this).parent("form") .... );
});
In your controller you would probably map it to an 'update' method
def update
# logic to update active attribute of model
render :nothing => true
end
精彩评论