Hi I have a field called "cost": <%= f.text_field :cost, :size=>2, :maxlength=>4 %> I want to check that user enter numeral value and this value is integer In post.rb (model): def cost_validate_int errors.add_to_base("Error message") unless cost.is_a?(Fixnum) puts "### #{cost}: "+cost.class.to_s end When I enter in field 100 I get: ### 100: Fixnum when I enter 10.1: ### 10: Fixnum when I enter ''asdf'': ### 0: Fixnum I completely can''t understand what happen. In migration: t.integer :cost I think ActiveRectord converts value to Fixnum because in migration this field signed as integer Can I in some way check cost value in model (value should be numeral and integer)? Thanks in advance! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Stanislav Orlenko wrote:> > Can I in some way check cost value in model (value should be numeral and > integer)? > > Thanks in advance!validates_numericality_of? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Pale Horse wrote:> Stanislav Orlenko wrote: >> >> Can I in some way check cost value in model (value should be numeral and >> integer)? >> >> Thanks in advance! > > validates_numericality_of?I use errors.add_to_base because I don''t want to show field''s name in the error message(i. e. Cost bla bla bla. web site is localized). -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2010 11:34, Stanislav Orlenko <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi > I have a field called "cost": > > <%= f.text_field :cost, :size=>2, :maxlength=>4 %> > > I want to check that user enter numeral value and this value is integer > > In post.rb (model): > > def cost_validate_int > errors.add_to_base("Error message") unless cost.is_a?(Fixnum) > puts "### #{cost}: "+cost.class.to_s > end > > When I enter in field 100 I get: > > ### 100: Fixnum > > when I enter 10.1: > > ### 10: Fixnum > > when I enter ''asdf'': > > ### 0: Fixnum > > I completely can''t understand what happen. In migration: > > t.integer :cost > > I think ActiveRectord converts value to Fixnum because in migration this > field signed as integerHow is the string you enter getting into the cost attribute? Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Colin Law wrote:> On 16 June 2010 11:34, Stanislav Orlenko <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> � � �errors.add_to_base("Error message") unless cost.is_a?(Fixnum) >> >> when I enter ''asdf'': >> >> ### 0: Fixnum >> >> I completely can''t understand what happen. In migration: >> >> t.integer :cost >> >> I think ActiveRectord converts value to Fixnum because in migration this >> field signed as integer > > How is the string you enter getting into the cost attribute? > > ColinI can''t understand why cost field value (10, 10.1 or ''asdf'') all time is Fixnum in model. In this model I try to validate cost, and can''t because all time I get only one type - Fixnum! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
well... in fact... you did defined it as an "integer" so, dont expect to get any other class thats not Fixnum or Bignum... but, you can also use some model validations to be sure that the user entered an integer... try validates_numericality_of :cost it will stop your user from entering strings.... i`m not sure how to stop it from entering floats like "10.6", but i`m sure there''s a simple way of doing it... you know, you''re programming in ruby on rails, it IS simple. Sempre Alerta Para Servir, Lucas Franceschi Equipe de Programação (Automação) SGI Sistemas (049) 9922-3360 On Thu, Jun 17, 2010 at 11:35 AM, Stanislav Orlenko <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Colin Law wrote: > > On 16 June 2010 11:34, Stanislav Orlenko <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> � � �errors.add_to_base("Error message") unless cost.is_a?(Fixnum) > >> > >> when I enter ''asdf'': > >> > >> ### 0: Fixnum > >> > >> I completely can''t understand what happen. In migration: > >> > >> t.integer :cost > >> > >> I think ActiveRectord converts value to Fixnum because in migration this > >> field signed as integer > > > > How is the string you enter getting into the cost attribute? > > > > Colin > > I can''t understand why cost field value (10, 10.1 or ''asdf'') all time is > Fixnum in model. In this model I try to validate cost, and can''t because > all time I get only one type - Fixnum! > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
try looking Here<http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001401> . Sempre Alerta Para Servir, Lucas Franceschi Equipe de Programação (Automação) SGI Sistemas (049) 9922-3360 On Thu, Jun 17, 2010 at 12:14 PM, lucas franceschi <lukas1596-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> well... in fact... you did defined it as an "integer" so, dont expect to > get any other class thats not Fixnum or Bignum... > > but, you can also use some model validations to be sure that the user > entered an integer... > > try validates_numericality_of :cost > it will stop your user from entering strings.... > > i`m not sure how to stop it from entering floats like "10.6", but i`m sure > there''s a simple way of doing it... > > you know, you''re programming in ruby on rails, it IS simple. > Sempre Alerta Para Servir, > > Lucas Franceschi > Equipe de Programação (Automação) > SGI Sistemas > (049) 9922-3360 > > > > On Thu, Jun 17, 2010 at 11:35 AM, Stanislav Orlenko <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > >> Colin Law wrote: >> > On 16 June 2010 11:34, Stanislav Orlenko <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> � � �errors.add_to_base("Error message") unless cost.is_a?(Fixnum) >> >> >> >> when I enter ''asdf'': >> >> >> >> ### 0: Fixnum >> >> >> >> I completely can''t understand what happen. In migration: >> >> >> >> t.integer :cost >> >> >> >> I think ActiveRectord converts value to Fixnum because in migration >> this >> >> field signed as integer >> > >> > How is the string you enter getting into the cost attribute? >> > >> > Colin >> >> I can''t understand why cost field value (10, 10.1 or ''asdf'') all time is >> Fixnum in model. In this model I try to validate cost, and can''t because >> all time I get only one type - Fixnum! >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.