Skip to content

Instantly share code, notes, and snippets.

@paydro
Created January 14, 2011 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paydro/780331 to your computer and use it in GitHub Desktop.
Save paydro/780331 to your computer and use it in GitHub Desktop.
Sample js_message javascript call
# 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
// 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