varidates_presence_ofの仕組みを追いかける

公開日: : Ruby on Rails

記事内に広告を含む場合があります。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。

railsでvalidをかけると、以下のようなメッセージが出力される。

以下の内容をご確認の上、再度お試し下さい。

* Nameを入力して下さい

これって、どんな仕組みになってるんだろう?ということで追いかけてみた。


<div class="errorExplanation" id="errorExplanation"><h2>以下の内容をご確認の上、再度お試し下さい。</h2><ul><li>Nameを入力して下さい</li></ul></div>

errorExplanationでmdfind。


% mdfind -onlyin "/opt/local/" "errorExplanation"
/opt/local/lib/ruby/gems/1.8/doc/actionpack-2.0.2/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.html
/opt/local/lib/ruby/gems/1.8/doc/actionpack-2.0.2/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.src/M000414.html
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/test/template/form_helper_test.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/test/template/active_record_helper_test.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/active_record_helper.rb
/opt/local/lib/ruby/gems/1.8/doc/actionpack-1.13.3/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.html
/opt/local/lib/ruby/gems/1.8/doc/actionpack-1.13.3/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.src/M001129.html
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/test/template/active_record_helper_test.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/active_record_helper.rb
/opt/local/lib/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb
/opt/local/lib/ruby/gems/1.8/doc/actionpack-1.13.6/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.html
/opt/local/lib/ruby/gems/1.8/doc/actionpack-1.13.6/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.src/M001136.html
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/test/template/active_record_helper_test.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/helpers/active_record_helper.rb
/opt/local/lib/ruby/gems/1.8/doc/actionpack-2.0.2/rdoc/classes/ActionView/Helpers/ActiveRecordHelper.src/M001525.html
% vim /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/helpers/active_record_helper.rb

errorExplanation で検索。


# Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
# given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
# provided.
#
# This <tt>DIV</tt> can be tailored by the following options:
#
# * <tt>header_tag</tt> - Used for the header of the error div (default: h2)
# * <tt>id</tt> - The id of the error div (default: errorExplanation)
# * <tt>class</tt> - The class of the error div (default: errorExplanation)
# * <tt>object_name</tt> - The object name to use in the header, or
# any text that you prefer. If <tt>object_name</tt> is not set, the name of
# the first object will be used.
#
# To specify the display for one object, you simply provide its name as a parameter. For example, for the +User+ model:
#
# error_messages_for 'user'
#
# To specify more than one object, you simply list them; optionally, you can add an extra +object_name+ parameter, which
# be the name in the header.
#
# error_messages_for 'user_common', 'user', :object_name => 'user'
#
# NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
# you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors
# instance yourself and set it up. View the source of this method to see how easy it is.
def error_messages_for(*params)
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
header_message = "#{pluralize(count, 'error')} prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved"
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

このerror_messages_forで色々とやっているようだ。深追いはまた今度。

関連記事

サンフランシスコのピア39にあるチャウダーズでクラムチャウダーを食す!

lolipop アップルの開発者向けイベント「WWDC2014」

ミスドのカルピスドーナツとカルピスポンデリングを食べてみた!

ミスドで期間限定のカルピスコラボ商品「カルピスドーナツ」と「カルピ

十三カレー計画で牛すじカレーネギのせを食す!(大阪・十三)

「iPhoneアプリ開発キャンプ@大阪」のランチで、十三カレー計画

大阪・難波の加寿屋 法善寺でかすうどんを食す。ランチタイムはおにぎり2個まで無料!

大阪・難波の加寿屋 法善寺 (かすうどん KASUYA)で、かす

ライブドアブログで運営していた「あきお商店」を「卵は世界である」に改名しました

少し前からライブドアブログで「あきお商店」というブログをやって

→もっと見る

PAGE TOP ↑