Undefined method `validates_presence_of' for #<ProductsContr


#1

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


#2

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


#3

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


#4

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


#5

======================================
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