This is a simple demo of running the same CoffeeScript code on both the client and server ends of a Rails 3.1 app. It was created to accompany my talk at RailsConf 2011.
If you'd like to learn more about CoffeeScript, check out the PragProg book, written by yours truly:
There are many times when it makes sense to run the same logic on both the client and the server. Validations are a classic example. Server-side validations are critical, but providing validations on the client side (as the user enters information) can provide a much better experience. Traditionally, developers have had three options:
- Skip client-side validations (saves time at the cost of user experience)
- Painstakingly implement the same validation logic on both the client and the server (great user experience, but lots of development time)
- Use Ajax (a decent middle ground, but not always practical)
(The client_side_validations plugin provides a solid fourth option—as long as all of your validations fit neatly into simple categories. You're still on your own for custom validation logic, like the password strength check used in this project.)
This app is a pretty minimal example of how to do that.
The meat of the app is these lines, which run after a signup form is submitted:
I'm hoping that plugins will make it easier to do this sort of thing in the future. But even in this trivial case, it's still less work than writing the same validations twice—once in CoffeeScript, once in Ruby—and worrying about them going out of sync.
The way feedback is provided if an error-riddled form is submitted is, currently, pretty lame. With a few hours of work, you could make the server-side feedback look and act like the client-side feedback. But why bother when there's jsdom, a server-side library for emulating a client-side environment? Before serving the page, you could intercept the HTML and manipulate it with jQuery using the same code as on the client side.
Currently, there's no plugin for this, so doing it would be a bit of a hack. But this technique may well be the future of validation—and even templating—in Rails.
Copyright (c) 2011 Trevor Burnham
Available under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.