Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
In this quick post I’ll share some code to submit a Ruby on Rails 4 model form via AJAX.
I created a new directory and added RVM files for Ruby version and gemset.
File: .ruby-version
File: .ruby-gemset
Installed Rails gem
Created a new Rails project
Generated the complete scaffolding for a Model called “Thing”.
Set up database (defaults to SQLite)
Added model validation, to test the ajax error response.
Revised the json format blocks for the update and create actions in the Things controller.
Revised the form, modified the form_for tag and error container html
Added AJAX listeners (coffeescript)
I started the server via “rails s”, and browsed to the url: http://localhost:3000/things/new
Submitting the form without entering a title will show the errors container. Submitting with a title will hide the errors and display a message that the Thing was saved.