Topic: error_messages_for does not display anything

i'm new to webdevelopment and rails, i'm currently reading a book, foundation rails 2 and i like it so far. however i bumped onto a small problem. for some reason when i deliberately create an erroneous record through the new action my error_messages_for does not display the errors. can someone explain why. i'm using rails 2.2.3, my code is posted below. any help is appreciated.

class Pirate < ActiveRecord::Base
  validates_presence_of :name, :ship
end

class PiratesController < ApplicationController
  def new
    @pirate = Pirate.new
  end

  def create 
    @pirate = Pirate.new(params[:pirate])
    if @pirate.save
      redirect_to :action => 'show', :id => @pirate.id
    else 
      redirect_to :action => 'new'
    end 
  end

end

<h1>Create a new Pirate</h1>

<%= error_messages_for :pirate %>

<% form_for :pirate, :url => {:action => :create} do |f| %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %><br />
  </p>

  <p>
    <%= f.label :ship %><br />
    <%= f.text_field :ship %><br />
  </p>

  <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>
<p><%= link_to 'Back', :action => :index %></p>

Re: error_messages_for does not display anything

Checl if your layout page contains the below lines to be able to display flash messages:

<% if flash[:notice]%>
    <div id="notice"><%= flash[:notice] %></div>
<% end %>

as well your CSS file to be sure that error message div and error fields displayed correctly.

Last edited by Javix (2010-12-20 12:10:32)