Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I separate validation error messages?
  2. <%= form_for @product do |f| %>
  3.   <% if @product.errors.any? %>
  4.       <ul id="error_explanation">
  5.       <% @product.errors.each_with_index do |msg, i| %>
  6.         <li><%= msg[1] %></li>
  7.       <% end %>
  8.       </ul>
  9.   <% end %>
  10.  
  11.   <fieldset>
  12.     <%= f.text_field :title, :placeholder => "Product Title", :title => "Type product title", :autofocus => 'autofocus' %>
  13.     <%= f.text_area :description, :placeholder => "Product Description", :title => "Type product description" %>
  14.     <%= f.collection_select :category_id, Category.all, :id, :name %>
  15.   </fieldset>
  16.  
  17.   <%= f.submit "Create", :class => "btn btn-big btn-action" %>
  18.   <%= link_to "cancel", categories_path %>
  19. <% end %>
  20.        
  21. class Product < ActiveRecord::Base
  22.   attr_accessible :title, :description, :price, :category_id
  23.  
  24.   validates :title, :presence => { :message => "All products need a title bro!" }
  25.   validates_uniqueness_of :title
  26.  
  27.   validates :description, :presence => { :message => "This ain't gonna be too interesting without a description?!" }
  28.   validates :price, :presence => { :message => "How you gonna make money if shit is free?!" }
  29.  
  30.   belongs_to :category
  31. end
  32.        
  33. <% if @product.errors.on(:title).present? %>
  34.   <span style="color:RED">WRITE YOUR ERROR MESSAGE</span>
  35. <% end %>