rails:118
From: Stoyan Zhekov <stoyan@g...>
Date: Mon, 28 Mar 2005 18:52:37 +0900
Subject: [rails:118] 日本語のアトリブートの名前
ストヤンです。 OSC2005で高橋征義と話したときそんな問題出ました: "どうやってvalidate使うときattributeのなまえも日本語で出る。 Example: ------------- app/models/category.rb ----------------- class Category < ActiveRecord::Base validates_length_of :name, :within => 5..10, :too_short => "が短すぎる", :too_long => "が長すぎる" end ---------- app/views/categories/_form.rhtml ---------- <%= error_messages_for 'category' %> ....... エラーmessages: "Name が短すぎる" と "Name が長すぎる" もっといいmessages: ------------- app/models/category.rb ----------------- # !!! attributeの名前もはいてる !!!! class Category < ActiveRecord::Base validates_length_of :name, :within => 5..10, :too_short => "名前が短すぎる", :too_long => "名前が長すぎる" end -------- app/helpers/application_helper.rb ---------- module ApplicationHelper def render_errors_for(object_name) object = instance_eval("@#{object_name}") unless object.errors.empty? err = '<div class="errorsBlock">' + '<h3>エラー:</h3>' + '<ul>' object.errors.each do |attr_name, message| err << "<li>#{message}</li>" end err << '</ul>' err << '</div>' end end end ---------- app/views/categories/_form.rhtml ---------- <%= render_errors_for 'category' %> ....... エラーmessages: "名前が短すぎる" と "名前が長すぎる"