ApplicationHelper.rb文件中追加如下方法: # Methods added to this helper will be available to all templates in the application. module ApplicationHelper def error_messages_for(object_name, options = {}) options = options.symbolize_keys object = instance_variable_get("@#{object_name}") if object && !object.errors.empty? content_tag("div", content_tag( options[:header_tag] || "h2", "データ#{object.class::ALIAS}を更新する時#{object.errors.count}個エラーが発生しました。" ) + content_tag("ul", object.errors.collect { |attr, msg| content_tag("li", object.class::COLUMN_ALIASES[attr] + msg) }), "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" ) else "" end end end Model层的定义如下: class Work < ActiveRecord::Base #has_one :user ALIAS = '勤務時間' COLUMN_ALIASES = { 'adate' => '日付: ', 'btime' => '作業開始時間: ', 'ctime' => '作業終了時間: ', 'content' => '作業内容: ' } validates_presence_of :adate, :btime, :ctime, :message => "該当データが入力してください!" validates_numericality_of :btime, :ctime, :message => "勤務時刻を半角数字で入力してください!" validates_uniqueness_of :adate, :scope => "ip", :message => "該当日付の勤務時間が既に登録した!" validate :validates_worktime_values protected def validates_worktime_values if btime.nil? || btime.length < 4 errors.add(:btime, '該当データを半角4桁で入力してください!') else if substr(btime,2,4).to_s != '00' && substr(btime,2,4).to_s != '30' errors.add(:btime, '該当データを30分単位で入力してください!') end end if ctime.nil? || ctime.length < 4 errors.add(:ctime, '該当データを半角4桁で入力してください!') else if substr(ctime, 2,4).to_s != '00' && substr(ctime,2,4).to_s != '30' errors.add(:ctime, '該当データを30分単位で入力してください!') end end end private def substr(str,start,length) str.split(//)[start,length] end end 参考资源: http://www.javaeye.com/topic/77773