Created
July 25, 2012 18:42
-
-
Save adriand/3177806 to your computer and use it in GitHub Desktop.
unobtrusive 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
# 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