Validatesのエラーメッセージが表示されない
なぜか出ないので迷走中
controllers/hoge_controller.rb
def create @hage = Hoge.new(params[:hoge]) if @hage.save flash[:notice]="新規登録されました" redirect_to :action=>:show, :id=>@hage else flash[:notice]="だめ" p "errors", @hage.errors render :action=>:new end end
models/hoge.rb
class Telecast < ActiveRecord::Base validates :name, :presence => true end
views/hoges/new.html.erb
<%= error_messages_for(:hoge) -%>
こんな状態で「name」を空っぽにしてCreateすると「氏名を入力してください」が出るはずがでない。
ターミナルには
#<ActiveModel::Errors:0x4bfd6c8 @base=#<Hoge id: nil,name: nil>, @message={name=>"を入力してください"}>
ここまでは表示される。うん。
予定では「error_messages_for(:hoge)」に
<div class="errorExplanation" id="errorExplanation"> <h2>ホゲに10つのエラーが発生しました。</h2> <p>次の項目を確認してください。</p> <ul><li>氏名を入力してください。</li></ul> </div>
こんなのが出るはずが出ない。
結果はコントローラーのスペルミス、「ホゲ」と書くところを「ハゲ」と書いていた。
ruby/rails/RailsGuidesをゆっくり和訳してみたよ/Active Record Validations and Callbacks