Thijs van der Vossen,
20 Nov 2007, 11:33 in ruby on rails and business (edit).
Please join us in welcoming Sam Aaron to our team.
Sam has been active in the Ruby and Rails communities for some years; he founded the Newcastle Ruby and Rails user group and was a speaker at RailsConf Europe this year. He enjoys writing and regularly publishes Ruby articles for InfoQ. Sam was also the technical reviewer for ‘Beginning Google Maps Applications with Rails and Ajax’ from Apress and a contributor to ‘The Rails Way’ published in the Addison-Wesley Professional Ruby Series .
In addition to being a fond advocate of both Ruby and Rails, Sam is interested in the aesthetics of programming languages, language oriented programming, and domain specific languages, which were the general subjects of his Ph.D. thesis.
Sam loves cycling (which is great here in Amsterdam) and also enjoys getting out into the countryside where he likes to walk, scramble and camp.
Norbert Crombach,
03 Nov 2007, 15:10 in ruby on rails (edit).
Even though Ryan Daigle already covered it in his usual timely fashion, I’d like to share some real life examples of how we use the relatively new rescue_from functionality.
Because I usually prefer methods that raise exceptions instead of returning a boolean, like, say, save! instead of just save, I thought it would be nice if I could deal with some common exceptions on a higher level. This lead me to write a Rails patch based on the exception_handler plugin, which I previously used in some projects.
A few weeks later the patch
found its way into Rails core, just in time for the 2.0 preview release. Now everybody can rewrite this:
class PostsController < ApplicationController
def create
@post = Post.create!(params[:post])
rescue ActiveRecord::RecordInvalid
render :action => :new
end
def update
@post = Post.find(params[:id])
@post.update_attributes!(params[:post])
rescue ActiveRecord::RecordInvalid
render :action => :edit
end
end
to something like this:
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordInvalid do |exception|
render :action => (exception.record.new_record? ? :new : :edit)
end
end
class PostsController < ApplicationController
def create
@post = Post.create!(params[:post])
end
def update
@post = Post.find(params[:id])
@post.update_attributes!(params[:post])
end
end
And it just works.