Getting started with node.js
Tonight I'm giving a talk to the London Javascript User Group where I'm introducing node.js.
My slides are available here: http://barkingiguana.com/~craig/talks/2010/javascript-london/getting-started-with-node-js. There's a script if you print it out or there's a video on Vimeo if you'd like to watch it.
If you have any feedback please feel free to leave a comment.
Handling error feedback from Ajax requests to Rails applications
Ajax is frequently used to provide a richer user experience. Why then are important error messages rarely handled properly in Ajax enabled applications? Handling errors gracefully and in a way that helps the visitor to solve them adds a really high quality feel to your application. We've got all the necessary machinery, all it takes is a little care and attention.
class FoosController < ApplicationController
def update
@foo = Foo.find(params[:id])
respond_to do |format|
if @foo.save
format.html do
flash[:info] = "Your foo has been created."
redirect_to @foo
end
format.js { head :ok }
else
format.html do
flash.now[:warning] = "I could not update the foo."
render :action => :edit
end
format.json do
head :unprocessable_entity, :json => @foo.errors.to_json
end
end
end
end
end
Using the above code we get a lovely fallback HTML based behaviour and when we work with Ajax we get to use JSON behaviours. When JSON is requested and something goes wrong we get back an array of arrays that takes the following form.
[
[ "attribute1", "error1", "error2" ],
[ "attribute2", "error3"]
]
Just think of the awesome things you could do with such rich feedback...
new Ajax.Request('/foo.json', {
method: 'PUT',
parameters: {
authenticity_token: window._token,
"foo[subject]": $F('foo_subject'),
"foo[body]" : $F('foo_body')
},
onSuccess: function(transport) {
// This is Web 2.0: celebrate with a yellow highlight.
},
onFailure: function(transport) {
var errors = transport.responseJSON;
errors.each(function(error) {
var attribute = error.shift();
var messages = error.join(", ");
var errorMessage = attribute + " " + messages;
var inputNode = $("foo_" + attribute);
if(inputNode) {
// Show that something is wrong with this field.
inputNode.addClassName("error");
// Do something better than an alert box. Alert boxes suck.
alert(errorMessage);
}
});
}
});
Ajax and the Rails request authenticity token
Rails 1.2.6 introduced CSRF protection in the form of an authenticity token which is a reasonably long string used to make sure that any PUT / POST / DELETE request you've made to an application was really generated by you (or at least your browser) doing something in the application and that you weren't tricked into submitting it by some nefarious third party.
Rails automatically adds this token to any forms generated by it's helpers, but when building rich Ajax applications it can be useful to be able to generate the Javascript by hand.
Fire this snippet into your layout just above including all the other Javascript files to get access to the authenticity token in Javascript and let you submit requests using Ajax.
<%= javascript_tag "window._token = '#{form_authenticity_token}';" %>
Now you can build Ajax requests that are allowed to do stuff to the application.
new Ajax.Request('/foo.json', {
method: 'PUT',
parameters: {
authenticity_token: window._token,
text: $F('foo_text')
}
/* callbacks omitted for brevity */
})
