Tips for noob''s

Marlon Moyer
Sat, 01 Oct 2005 04:11:30 -0000

Need to let a user enter a price and don''t want to impose silly rules
about what''s valid?  Here''s what I''ve figured out

In your _form.rhtml, replace the standard text_field with this type:

<%= text_field_tag ''listing[price]'',
number_to_delimiter(@listing.price)  %>

you could also use number_as_currency too if you wanted a dollar sign
in your field.

next, in your model, add override the default before validation method.

def before_validation
  if self.price != nil

self.price_before_type_cast.to_s.gsub!(/[^0-9\.\-]/,'''').to_i
  end
end

This will remove anything that isn''t a number, decimal point, or minus
sign.



--
Marlon