Skip to content

Instantly share code, notes, and snippets.

@adriand
Created July 25, 2012 18:42
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 adriand/3177806 to your computer and use it in GitHub Desktop.
Save adriand/3177806 to your computer and use it in GitHub Desktop.
unobtrusive AJAX
# in the view
= form_for([:forge, @job, job_application], :remote => true) do |f|
# in the JS header portion of the view
$("form").on("ajax:success", function(data, status, xhr) {
alert("Success");
});
# in the controller, just make sure you handle format.js, and use render :nothing => true, NOT render :text => "Success"
def update
@job_application.resume = nil if params[:remove_asset] == "1"
if @job_application.update_attributes(params[:job_application])
respond_to do |format|
format.html do
flash[:notice] = 'Job application was successfully updated.'
redirect_to(forge_job_job_applications_path(@job_application.job_id))
end
format.js { render :nothing => true } # caution: render :text => "Success" will not trigger ajax:success
end
else
respond_to do |format|
format.html { render :action => "edit" }
format.js { render :text => "Failure", :status => 500 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment