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: [code]validates_numericality_of params[:size][/code] but then I get the following error: undefined method `validates_numericality_of'' for #<ReportLinesController:0x44ef120>. What should I do? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: > > [code]validates_numericality_of params[:size][/code] > > 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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: > > [code]validates_numericality_of params[:size][/code] > > but then I get the following error: > > undefined method `validates_numericality_of'' for > #<ReportLinesController:0x44ef120>. > > What should I do? > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Roger Pack 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 SchunselaarNo, because I dont want to use a database, but save the value in a plain text file. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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- s.net> wrote:> Roger Pack 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. ;-) Think of it as rails'' way of encouraging you to do your validations in the model. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Sjoerd Schunselaar Sent: Monday, May 05, 2008 5:40 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] 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: [code]validates_numericality_of params[:size][/code] but then I get the following error: undefined method `validates_numericality_of'' for #<ReportLinesController:0x44ef120>. What should I do? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Excellent! Keep spreading the good word about Ruby and Rails! On May 13, 7:32 am, Sjoerd Schunselaar <rails-mailing-l...@andreas- s.net> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---