PDA

View Full Version : validation message - how do i print it out?


eiji
March 12th, 2006, 07:46 AM
hi there.

uhh.. i have this "validates_presence_of" and some other validations (seen below) and i have these message right after.. but how do i print them out once they occur? the validations are being catched nad the view is showing a box-highlight on the textbox that had a validation process but how do i print out the message?

/model/ - validation occurs here
class Memberaccount < ActiveRecord::Base
#database reletionship
belongs_to :user
has_many :accesspoints

#validation
validates_uniqueness_of :username, :message => "username already exists"
validates_format_of :username, :with => /^\w+$/, :message => "cannot contain whitespace"
validates_presence_of :username, :password, :firstname, :lastname, :email, :message => "Missing required field"
end

/view/ - how do i print out the messages?
<h1>SignUp Form</h1>
<form method="post" action="/signup/save">
<table width="200" border="0">
<tr>
<td><label class="required" for="memberaccount_username">*Username: </label></td>
<td><%= text_field 'memberaccount', 'username' %></td>
</tr>
<tr>
<td>*Password: </td>
<td><%= password_field 'memberaccount', 'password' %></td>
</tr>



the view is just a portion because it only repeats itself but on different fields.

thanks for the help :D

rob
March 13th, 2006, 08:34 PM
Use:


<%= error_messages_for :user %>


Assuming, in this example, that the model is "user".

eiji
March 14th, 2006, 01:53 AM
wow thanks a lot rob!!

but i wonder why the one on the _form.rhtml isnt getting rendered in my index.rhtml

o its because i havent used the "<%= render :partial => 'form' %>" in it. one question,

=> 'form' is equal to _form.rhtml??

rob
March 15th, 2006, 03:36 AM
A partial needs to be named _form.rhtml to be rendered as the partial named "form".

eiji
March 16th, 2006, 07:02 AM
i see.

thanks again rob. :D

umn just one follow up. have you seen my other post, transactional stuff?? do you know how that could work?

rob
March 16th, 2006, 06:41 PM
I haven't had to do transactions yet in Rails.

I recommend strongly though you buy the book Agile Web Development with Ruby on Rails (http://www.amazon.com/exec/obidos/redirect?link_code=ur2&tag=ocssolutions-20&camp=1789&creative=9325&path=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fproduct%2F 097669400X%2Fsr%3D8-1%2Fqid%3D1142552356%2Fref%3Dpd_bbs_1%3F%255Fencod ing%3DUTF8). This has a great section on transactions, and is a wonderful and almost must for any Rails developer.

eiji
March 20th, 2006, 11:10 AM
hi rob,

first, i would like to thank you for helping me out, a lot. and also to motobass. :D thanks! :D

uhh.. this is regarding the validation again..

i got the one to work.. the validations, and it was working fine until i added another error_message for another database. now im having this error and i dont know if i had missed something..

error is below..

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occured while evaluating nil.errors

and it always points out to that error_messaging
3: <%= error_messages_for :persona %>

and also, now the one i was working on before is having errors also.. :(

do you know where i have went wrong..?

thanks in advance..