子在川上曰

            逝者如斯夫不舍晝夜
          隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
          數(shù)據(jù)加載中……

          error_messages_for的中文化

          在Rails中error_messages_for為國際化的考慮不是很周全,它顯示的是模型的字段名稱,這對于用戶來說是不友好的。為了解決這個問題,只能改它的源碼了----將如下方法,放入ApplicationHelper
          文/陳剛? www.chengang.com.cn? 轉載請聲明出處

          ? def?error_messages_for(*params)
          ????
          #add?by?glchengang
          ????key_hash?=?{}
          ????
          if?params.first.is_a?(Hash)
          ??????key_hash?
          =??params.first
          ??????params
          .delete_at(0)
          ????end
          ????
          #add?end

          ????options?
          =?params.last.is_a?(Hash)???params.pop.symbolize_keys?:?{}
          ????objects?
          =?params.collect?{|object_name|?instance_variable_get("@#{object_name}")?}.compact
          ????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
          ??????
          #?change?by?glchengang
          ??????header_message?=?"有#{count}個錯誤"
          #???????header_message?=?"#{pluralize(count,?'error')}?prohibited?this?#{(options[:object_name]?||?params.first).to_s.gsub('_',?'?')}?from?being?saved"
          ??????
          ??????#add?by?glchengang

          ??????error_messages?=?objects.map?do?|object|
          ????????temp?
          =?[]
          ????????object
          .errors.each?do?|attr,?msg|
          ??????????temp?
          <<?content_tag(:li,?(key_hash[attr]?||?attr)?+?msg)?
          ????????end
          ????????temp
          ??????end
          ??????
          #add?end

          #????????error_messages?=?objects.map?{|object|?object.errors.full_messages.map?{|msg|?content_tag(:li,?msg)?}?}

          ??????content_tag(:div,
          ????????content_tag(options[
          :header_tag]?||?:h2,?header_message)?<<
          #???????????content_tag(:p,?'There?were?problems?with?the?following?fields:')?<<
          ??????????content_tag(:ul,?error_messages),
          ????????html
          ??????)
          ????
          else
          ??????
          ''
          ????end
          ??end


          使用依然兼容老的方式,你也可以傳入一個哈希表,把模型字段顯示成對應的中文,示例如下:
          <%=?
          h?
          =?{'username'=>'用戶名',?'password'=>'密碼'}
          error_messages_for?h
          ,?:user
          %>

          另外,還要在environment.rb的最后插入以下代碼:

          errors?=?ActiveRecord::Errors.default_error_messages
          errors[
          :taken]?=?'已經(jīng)被使用'
          errors[
          :blank]?=?'不能為空'

          posted on 2007-09-19 17:35 陳剛 閱讀(1832) 評論(0)  編輯  收藏 所屬分類: Rails&Ruby

          主站蜘蛛池模板: 永定县| 阿勒泰市| 元氏县| 许昌市| 聊城市| 凌海市| 三亚市| 中超| 搜索| 商洛市| 奎屯市| 阿克苏市| 乌鲁木齐市| 青神县| 山阳县| 华亭县| 阿拉善左旗| 新竹县| 泸水县| 崇仁县| 调兵山市| 台南县| 祁东县| 宁河县| 贺州市| 孙吴县| 嘉峪关市| 余干县| 外汇| 江源县| 开远市| 苏尼特右旗| 达日县| 通化县| 蓬莱市| 威信县| 桐梓县| 邢台县| 大宁县| 准格尔旗| 南京市|