Validating integer in controller

I’m teaching Ruby on Rails right now, and I will save some values to a
plain text file.
That works, but I want to validate the variables. Normally I put the
validation in the model, but right now I want to check the variables
before saving into the text file. I tried to use:

validates_numericality_of params[:size]

but then I get the following error:

undefined method `validates_numericality_of’ for
#ReportLinesController:0x44ef120.

What should I do?

Sjoerd Schunselaar wrote:

I’m teaching Ruby on Rails right now, and I will save some values to a
plain text file.
That works, but I want to validate the variables. Normally I put the
validation in the model, but right now I want to check the variables
before saving into the text file. I tried to use:

validates_numericality_of params[:size]

but then I get the following error:

undefined method `validates_numericality_of’ for
#ReportLinesController:0x44ef120.

What should I do?

I’ve now fixed it with a custom method:

def isInteger()
print (params[:aantal].to_i)
if ((params[:aantal].to_i) != 0)
print “gaaaa”
return true

else
  print "gooo"
  return false
end

Is there something better?

Roger P. wrote:

validates_numericality_of
should work for classes that inherit from ActiveRecord::Base. Does
yours?

On Mon, May 5, 2008 at 6:40 AM, Sjoerd Schunselaar

No, because I dont want to use a database, but save the value in a plain
text file.

validates_numericality_of
should work for classes that inherit from ActiveRecord::Base. Does
yours?

On Mon, May 5, 2008 at 6:40 AM, Sjoerd Schunselaar

The important point is that the validation logic comes from
ActiveRecord, not ActionController, so you can’t just add a validation
call like validates_numericality_of anywhere you want. From a big-
picture perspective this makes sense: validation is business logic and
is properly separated into the model. If you’re teaching RoR and not
taking these MVC concepts into consideration you really need to revise
things quickly.

You might be able to mix in the ActiveRecord::Validations module into
your models so that you can get ActiveRecord-like validation without
actually using ActiveRecord. Another alternative might be to consider
writing a text-file-adapter for ActiveRecord but I suspect you don’t
have time for that.

Good luck.

On May 5, 9:36 am, Sjoerd Schunselaar <rails-mailing-l…@andreas-

That validator method is defined in ActiveRecord–from which all your
models will derive. So it’s available in models no problem, but if you
want to bring it in to a controller, you’re going to have to torture the
framework. :wink:

Think of it as rails’ way of encouraging you to do your validations in
the model.

AndyV wrote:

The important point is that the validation logic comes from
ActiveRecord, not ActionController, so you can’t just add a validation
call like validates_numericality_of anywhere you want. From a big-
picture perspective this makes sense: validation is business logic and
is properly separated into the model. If you’re teaching RoR and not
taking these MVC concepts into consideration you really need to revise
things quickly.

You might be able to mix in the ActiveRecord::Validations module into
your models so that you can get ActiveRecord-like validation without
actually using ActiveRecord. Another alternative might be to consider
writing a text-file-adapter for ActiveRecord but I suspect you don’t
have time for that.

Good luck.

On May 5, 9:36�am, Sjoerd Schunselaar <rails-mailing-l…@andreas-

Thank you, for your answer! I changed it and use a model for the file
load en saving so I can validate the value.

Excellent! Keep spreading the good word about Ruby and Rails!

On May 13, 7:32 am, Sjoerd Schunselaar <rails-mailing-l…@andreas-