Created
January 14, 2011 21:52
-
-
Save paydro/780331 to your computer and use it in GitHub Desktop.
Sample js_message javascript call
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
# corresponding rails action that handles the js_message call. Note the format.jsm calls in the | |
# respond_to block | |
def create | |
@task = Task.new(params[:task]) | |
respond_to do |format| | |
if @task.save | |
format.html { redirect_to(tasks_path, :notice => 'Task was successfully created.') } | |
format.jsm do | |
render_js_message(:ok, :html => "it saved!") | |
end | |
else | |
format.html { render :action => "new" } | |
format.jsm { render_js_message(:error, :message => "Task could not be saved. Please check your form") } | |
end | |
end | |
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
// Example of using js_message to send a form's data to a rails server | |
// This assumes jQuery | |
$(function(){ // jQuery's onload function | |
$("form.task-form").submit(function(){ | |
var form = $(this); | |
$.jsMessage({ | |
url: form.attr("action"), | |
data: form.serialize(), | |
success: function(jsMessage){ | |
$("#tasks").replaceWith(jsMessage.html); | |
}, | |
error: function(jsMessage){ | |
alert(jsMessage.message); | |
} | |
}); | |
return false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment