hi there. Here is the simple scenario: My active record connects to a table that contains an integer field. My form contains a text box that allows me to enter a value to get updated to the integer field. If anything gets entered that is anything other than a POSITIVE INTEGER, I want to display an error. I have noticed that if I create a new active record like @record = MyClass.new(params[:number]) and the number is a float, say 4.06, this will get rounded in the Active Record if i display it at this point render_text @record.number ...will show 4 Because it will get rounded automatically, I can''t tell that it is a non-integer number from within the active record and display an error. Also, if after submitting the form, the active record will contain the rounded value (4) but on return to the form, the text field will contain the original value submitted, 4.06. How can I intervene this behaviour, ie, test weather the value submitted is an INTEGER and return an error if it is not (including decimal values). -- Posted via http://www.ruby-forum.com/.
Hi, I would recommend adding the following to your controller: validates_numericality_of :my_positive_integer, :gte => 0, :only_integer => true Also, I would recommending getting and reading "Agile Web Development with Rails". Good luck, -Conrad On 6/19/06, montyman <simon_domino@yahoo.com> wrote:> > hi there. Here is the simple scenario: > > My active record connects to a table that contains an integer field. > > My form contains a text box that allows me to enter a value to get > updated to the integer field. > > If anything gets entered that is anything other than a POSITIVE INTEGER, > I want to display an error. > > I have noticed that if I create a new active record like > @record = MyClass.new(params[:number]) > > and the number is a float, say 4.06, this will get rounded in the Active > Record if i display it at this point > render_text @record.number ...will show 4 > > Because it will get rounded automatically, I can''t tell that it is a > non-integer number from within the active record and display an error. > > Also, if after submitting the form, the active record will contain the > rounded value (4) but on return to the form, the text field will contain > the original value submitted, 4.06. > > How can I intervene this behaviour, ie, test weather the value submitted > is an INTEGER and return an error if it is not (including decimal > values). > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060619/0c83c39d/attachment.html
Thanks for the solution, so simple. I have actually read the agile book (most of it), but i''m wondering if there is a reference where I can self discover all the various switches to these methods, for example the gte in validates_numericality_of. I couldn''t see this option in the online api reference. Thanks for the help. -- Posted via http://www.ruby-forum.com/.
Hey Monty, if you would like to use the string ":gte => 0", you''ll have to download and install the following plugin: http://www.umesd.com/better_validates_numericality_of.zip Otherwise, the following should work for you: validates_numericality_of :name, :only_integer => true validates_format_of :name, :with => /^([0-9]+)$/, :on => :create, :on => :update Finally, you would add that validation line to your model file instead of your controller. Good luck, -Conrad On 6/19/06, montyman <simon_domino@yahoo.com> wrote:> > Thanks for the solution, so simple. > > I have actually read the agile book (most of it), but > i''m wondering if there is a reference where I can self > discover all the various switches to these methods, > > for example the gte in validates_numericality_of. I couldn''t see > this option in the online api reference. > > Thanks for the help. > > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060620/397a084b/attachment.html