<% @lunch.errors[:name].each do |msg| %>@lunch.errors[:name]というようにしてエラーメッセージの情報を取得しています。errorsのオブジェクトは、full_messagesの他にもメッセージを取得するための仕組みがあるのです。errors[:コラム]というようにコラム名を指定することで、そのコラムで発生したエラーメッセージだけを取り出すことができます。
<br /><%= msg %>
<% end %>
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
※Lunchモデルのスクリプト # coding: utf-8 class Lunch < ActiveRecord::Base attr_accessible :genre, :memo, :name, :star validates :name, :presence => {:message => "nameは必須項目です。"} validates :star, :numericality => { :only_integer => true, :message => "starは整数で入力ください。" } end ※_form.html.erbのリスト <%= form_for(@lunch) do |f| %> <% if @lunch.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@lunch.errors.count, "error") %> prohibited this lunch from being saved:</h2> </div> <% end %> <div class="field"> <pre><% @lunch.errors[:name].each do |msg| %> <br /><%= msg %> <% end %></pre> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="field"> <pre><% @lunch.errors[:star].each do |msg| %> <br /><%= msg %> <% end %></pre> <%= f.label :star %><br /> <%= f.number_field :star %> </div> <div class="field"> <%= f.label :genre %><br /> <%= f.text_field :genre %> </div> <div class="field"> <%= f.label :memo %><br /> <%= f.text_area :memo %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
<< 前へ | 次へ >> |