Consider this bit of code:
... I'm well off here, I know. When date validation fails I see a RuntimeError in the controller and the string specified in the News.validate_date method.Code:## News model class News < ActiveRecord::Base before_create :validate_date def validate_date raise "Invalid creation date..." if created_on > Time.now end end ##News controller class Publish::NewsController < Publish::BaseController def create begin @news = News.new(params[:news]) rescue flash[:notice] = "Invalid creation date" render :action => 'new' end if @news.save flash[:notice] = 'News was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end end
Can someone explain how to properly deal with raising exceptions in the model and rescuing in the controller? Much appreciated...





Bookmarks