Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Demo of using CoffeeScript in Rails 3.1 on both the client and server ends

branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.mdown

CoffeeScript End-to-End

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:
http://coffeescript-book.com/

Why?

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:

  1. Skip client-side validations (saves time at the cost of user experience)
  2. Painstakingly implement the same validation logic on both the client and the server (great user experience, but lots of development time)
  3. 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.)

But now, you have a fourth option: A project called ExecJS by 37signals' own Sam Stephenson provides a bridge from Ruby to JS. Which means you can write your validation logic just once—in CoffeeScript or JavaScript—and both serve it to browsers and run it on your Rails server.

This app is a pretty minimal example of how to do that.

How?

The meat of the app is these lines, which run after a signup form is submitted:

validation_cs = File.read 'app/assets/javascripts/validation.js.coffee'
validation_js = CoffeeScript.compile validation_cs
validation_context = ExecJS.compile validation_js

validation_error = validation_context.call 'validate.all', params

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.

TODO

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

Copyright (c) 2011 Trevor Burnham
http://trevorburnham.com

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.

Something went wrong with that request. Please try again.