Forum: Ruby on Rails undefined method `validates_presence_of' for #<ProductsContr

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Prashant T. (Guest)
on 2006-03-13 09:35
Hi,

I am trying to use method `validates_presence_of' for validating my
input fields on form . I m using it in my action class as follows:-
=======================
validates_presence_of(params[:product][:quantity])
======================

But when I am running my application i m getting error like:-
=========================
undefined method `validates_presence_of' for #<ProductsContr
=========================

What is problem here??

Also I want to validate the field on my form (quantity) for its value
less than 10. if value entared in quantity field in my form is less than
10 then it should check it & should give validation message on my view.

How to do this????

Thanx.
Prash
joey__ (Guest)
on 2006-03-13 09:43
Prashant T. wrote:
> Hi,
>
> I am trying to use method `validates_presence_of' for validating my
> input fields on form . I m using it in my action class as follows:-
> =======================
> validates_presence_of(params[:product][:quantity])
> ======================
>
> But when I am running my application i m getting error like:-
> =========================
> undefined method `validates_presence_of' for #<ProductsContr
> =========================
>
> What is problem here??
>
> Also I want to validate the field on my form (quantity) for its value
> less than 10. if value entared in quantity field in my form is less than
> 10 then it should check it & should give validation message on my view.
>
> How to do this????
>
> Thanx.
> Prash
This should go in the model.
validates_presence_of :quantity

This is about validating data, it should b in the model not controller.

Joey
http://www.feedreed.com
Prashant T. (Guest)
on 2006-03-13 10:03
Hi joy,

In which part of model should i write this 'validates_presence_of'
method. Currently my model is as follows:-
======================================
class Product < ActiveRecord::Base
	attr_reader :products
	attr_reader :quantiti

	def add_product(product,quantity)
	 	products=Product.find(product.id)
	end
end
=======================================

In above model where should i write code for 'validates_presence_of'
method??//

Thanx.

Prash
Arpan (Guest)
on 2006-03-13 10:49
======================================
class Product < ActiveRecord::Base
	attr_reader :products
	attr_reader :quantiti

	validates_presence_of :quantity

	def add_product(product,quantity)
	 	products=Product.find(product.id)
	end
end
=======================================

That ought to do it.
Arpan
Prashant T. (Guest)
on 2006-03-13 11:04
Arpan wrote:
> ======================================
> class Product < ActiveRecord::Base
> 	attr_reader :products
> 	attr_reader :quantiti
>
> 	validates_presence_of :quantity
>
> 	def add_product(product,quantity)
> 	 	products=Product.find(product.id)
> 	end
> end
> =======================================
>
> That ought to do it.
> Arpan

I tried with the same thing as u told. But I am not getting any error
message or something like textbox with coloured borders for the fields
which were blank.
My Database table name is 'products' & contains a field named as
'demand' with datatype varchar. My view contains 3 textboxes named
"demand1","demand2","demand3". Now when I click on submit button on form
it should check for form fields against 'validates_presence_of' method.
So I ve written in my model class as follows:-
==============
class Product < ActiveRecord::Base
        validates_presence_of :demand
end
================

But I am not getting ay error message even if I insert blank value in
textbox & blank value gets added into database.

Where is problem????

Thanx.
Prash
This topic is locked and can not be replied to.