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
--
Posted via http://www.ruby-forum.com/.
Yngwie wrote:> 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, > YngA 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? Thanks in advance, Yng -- Posted via http://www.ruby-forum.com/.
Yngwie wrote:> 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, > YngA 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? Thanks in advance, Yng -- Posted via http://www.ruby-forum.com/.
"Yngwie" <mdalsant@gmail.com> wrote in message news:f098a0aab809af4ae41b295699e20351@ruby-forum.com... Hi yng I''m not sure what you mean by "nothing worthwhile" in the api docs. At least for validations, the documentation is very good: http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html> i.e. --> > class Record < ActiveRecord::Base > validate_presence_of > endit should be validates_presence_of, (you''re missing an s on ''validate'') and 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
> 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 > alanHey 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? Thxs, Yngwie -- Posted via http://www.ruby-forum.com/.
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.
- dan
--
Dan Kohn <mailto:dan@dankohn.com>
<http://www.dankohn.com/> <tel:+1-415-233-1000>
On Jul 12, 2006, at 7:46 AM, Yngwie wrote:
> 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?