Is there an expert validater that can point me in the right direction?
I’ve looked for extended documentation on ‘validates_presence_of’ but i
can’t seem to find any.
I’m trying to make sure that i don’t enter a nil object in my database,
so i’ve added validate_presence_of to my Record Model.
i.e. -->
class Record < ActiveRecord::Base
validate_presence_of
end
I’m coming up with errors when trying this. Do i need to create a
function to handle the situation when i try leaving a field blank? Does
any one know of a good place to find detailed info on the validates
methods (Please don’t point me to the api, which has nothing worth while
there.
Is there an expert validater that can point me in the right direction?
I’ve looked for extended documentation on ‘validates_presence_of’ but i
can’t seem to find any.
I’m trying to make sure that i don’t enter a nil object in my database,
so i’ve added validate_presence_of to my Record Model.
i.e. -->
class Record < ActiveRecord::Base
validate_presence_of
end
I’m coming up with errors when trying this. Do i need to create a
function to handle the situation when i try leaving a field blank? Does
any one know of a good place to find detailed info on the validates
methods (Please don’t point me to the api, which has nothing worth while
there.
Thanks,
Yng
A little more info w/ details ==>
I have an edit .rhtml page which has the user enter ‘record’ data
fields. When I go to update these fields, i’m directed to the Update
controller which looks like
def update @record.Record.find(params[‘id’])
if( @record.update_attributes(params[‘record’])
redirect_to :action => ‘view’
else
redirect_to :action => ‘edit’
end
end
So when i leave a field, that i’m validataing presence of, blank the
record doesn’t save and it goes directly to the else statement
(redirect_to :action => ‘edit’) How do I get the page to reload and
prompt the user to fill in the blank fields?
Is there an expert validater that can point me in the right direction?
I’ve looked for extended documentation on ‘validates_presence_of’ but i
can’t seem to find any.
I’m trying to make sure that i don’t enter a nil object in my database,
so i’ve added validate_presence_of to my Record Model.
i.e. -->
class Record < ActiveRecord::Base
validate_presence_of
end
I’m coming up with errors when trying this. Do i need to create a
function to handle the situation when i try leaving a field blank? Does
any one know of a good place to find detailed info on the validates
methods (Please don’t point me to the api, which has nothing worth while
there.
Thanks,
Yng
A little more info ==>
I have an edit .rhtml page which has the user enter ‘record’ data
fields. When I go to update these fields, i’m directed to the Update
controller which looks like
def update @record.Record.find(params[‘id’])
if( @record.update_attributes(params[‘record’])
redirect_to :action => ‘view’
else
redirect_to :action => ‘edit’
end
end
So when i leave a field, that i’m validataing presence of, blank the
record doesn’t save and it goes directly to the else statement
(redirect_to :action => ‘edit’) How do I get the page to reload and
prompt the user to fill in the blank fields?
you need to specify which attributes you are trying to validate.
Here’s an example from the api docs:
class Person < ActiveRecord::Base
validates_presence_of :first_name
end
hth
alan
Hey Alan,
thanks for the timely response,
I misspelled validates on this blog, but it’s was correctly spelled in
my record model. When i said the api wasn’t worthwhile, i meant that
the simple example they have doesn’t do me any good. I try to follow
the model, and ‘validates_presence_of’ just doesn’t allow me to save the
record. I need the user to be prompted to enter in a valid field. any
insight?
You may be asking how to display the validation errors to the user.
In that case, you want to use error_messages_for <http:// api.rubyonrails.com/classes/ActionView/Helpers/
ActiveRecordHelper.html#M000460> in the view.
The easiest way to learn this is to use “/script/generate scaffold
Test” and look at the code generated. It has nice examples of using
validations and displaying errors to users.