错误提示中文标题


 
  # Added By: 宋鹏, 错误提示中文标题
  # TODO 没有实现对多个对象的错误显示支持 error_messages_for(object_name,object_names=nil,options = {})
  def error_messages_for(object_name,options = {})
    options = options.symbolize_keys
    object = instance_variable_get("@#{object_name}")
    ob_class = object.class
    unless object.errors.empty?
      error_lis = []
      object.errors.each{ |key, msg| error_lis << content_tag("li", ob_class.human_attribute_name(key)+": "+msg) }
      content_tag("div", content_tag(options[:header_tag] || "h2", "发生#{object.errors.count}个错误" ) + content_tag("ul", error_lis), "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" )
    end
  end
  # 以下是active_record_helper.rb源代码,支持多个对象的错误显示
  def error_messages_for(*params)
    options = params.extract_options!.symbolize_keys
   
    if object = options.delete(:object)
      objects = [object].flatten
    else
      objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
    end
   
    count  = objects.inject(0) {|sum, object| sum + object.errors.count }
    unless count.zero?
      html = {}
      [:id, :class].each do |key|
        if options.include?(key)
          value = options[key]
          html[key] = value unless value.blank?
        else
          html[key] = 'errorExplanation'
        end
      end
      options[:object_name] ||= params.first
   
      I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale|
        header_message = if options.include?(:header_message)
          options[:header_message]
        else
          object_name = options[:object_name].to_s.gsub('_', ' ')
          object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1)
          locale.t :header, :count => count, :model => object_name
        end
        message = options.include?(:message) ? options[:message] : locale.t(:body)
        error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, ERB::Util.html_escape(msg)) } }.join
   
        contents = ''
        contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
        contents << content_tag(:p, message) unless message.blank?
        contents << content_tag(:ul, error_messages)
   
        content_tag(:div, contents, html)
      end
    else
      ''
    end
  end

 

-------------------------------------------------------------------------------------------------

I18N 定制中文error message

 

使用RAILS进行开发...长话短说。(版面难看了些,将就些吧,只要内容能解决问题就好了。)

具体例子 Model: User

#先加一些验证

validates_presence_of :account #:message => '这里先不写'

validates_uniqueness_of :account, :if => Proc.new { |elvuel| !elvuel.account.blank? } #:message => '也不写'

validates_presence_of :password

...

这样在save(当然可以指定on :create 或 :update)操作的时候,未通过验证时,将出现信息提示。不过这些提示是英文的。

类似: 3 errors prohibited this user from being saved

Account : ......(eng)

Password: ...(eng)

我们要中文的错误提示!!!要解决这个问题,可以给 error_messages加入 :header_tag :object_name :header_message这些参数。不过仍不能很好的解决问题,部分信息仍旧是英文的;而且每次都要设置一些属性,浪费时间!DRY---

让i18n出场,config目录locales目录建cn.yml(当然也可以cn.rb,依照各人喜好),节约打字时间,这里不讲i18n怎么设置。

首先说: 3 errors prohibited this user from being saved这个怎么修改成中文。

yml:activerecord.errors.template.header

activecord:

  errors:

     template:

        header: "这个{{model}}在处置的时候: 出现{{count}}个错误."""

  body: "信息如下:"

然后activerecord.models.user(这个user就是modelname)

activerecord:

   models:

      user:

         one: "就是用户"

         other: "count数量{{count}}个用户"

再接着那些属性的account,password这类的改成中文信息

activerecord.attributes.user.account  activerecord.attributes.user.password

activerecord:

   attributes:

     user:

       account: "你说是用户名或是帐号都随便"

  password: "密码"

看起来可以了,稍等,那些Msgs还没有变成中文...

activereocrd.errors.messages.....

errors:

 messages:
   inclusion: "不包含于列表中"
   exclusion: "不能用"
   invalid: "不合法"
   confirmation: "与确认值不匹配"
   accepted: "必须接受"
   empty: "不能留空"
   blank: "不能为空字符"
   too_long: "过长 (不能超过 {{count}} 个字符)"
   too_short: "过短 (不能少于 {{count}} 个字符)"
   wrong_length: "长度非法 (必须为 {{count}} 个字符)"
   taken: "不可用"
   not_a_number: "不是数字"
   greater_than: "必须大于 {{count}}"
   greater_than_or_equal_to: "必须大于或等于 {{count}}"
   equal_to: "必须等于 {{count}}"
   less_than: "必须小于 {{count}}"
   less_than_or_equal_to: "必须小于或等于 {{count}}"
   odd: "必须为单数"
   even: "必须为双数"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值