Display validation errors individually


I hope someone can help me with a problem.

Given the following code:

class User < ActiveRecord::Base
validates_uniqueness_of :username, :message => “Username is taken”
validates_presence_of :username
validates_presence_of :password

<%= error_messages_for ‘user’ %>
<%= form_tag %>

Username: <%= text_field("user", "username") %> I WANT TO DISPLAY USERNAME ERRORS HERE
Password: <%= password_field("user", "password") %> I WANT TO DISPLAY PASSWORD ERRORS HERE
<%= end_form_tag %>

I don’t want to display all of the errors in one block using <%=
error_messages_for ‘user’ %>

How can I display error messages for specific form fields individually
as indicated in the HTML above?

Thank you so much for your help.


On May 6, 2007, at 8:21 PM, Chris wrote:

Username: <%= text_field("user", "username") %> I WANT TO DISPLAY USERNAME ERRORS HERE

The starting point is @user.errors.on(:username).

– fxn

Thanks for your help. I have now solved the problem.

For anyone else experiencing the same problem, here is the code:

<%= form_tag :action => ‘do_something’ %>

Username: <%= text_field("user", "username") %> <%= error_message_on("user", "username") %>
Password: <%= password_field("user", "password") %> <%= error_message_on("user", "password") %>
<%= end_form_tag %>

class MyController < ApplicationController

def do_something
@user = User.new(@params[‘user’])
if @user.save
# User was saved
# User was not saved so redisplay the form


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs