gsub within before_validation method is zeroing out my value
I'm trying to remove dollar signs and commas from my form input (for example, $1,000.00 => 1000.00)
I have the following following line in my before_validation method in my model:
self.parents_mortgage = self.parents_mortgage.to_s.g开发者_如何学JAVAsub!('$,','').to_i
This is causing any number to be put through to zero out. Is there something wrong with my syntax?
use tr
:
self.parents_mortgage = self.parents_mortgage.to_s.tr!('$,','').to_i
self.parents_mortgage = self.parents_mortgage.to_s.gsub!('$','').gsub!(',','').to_i
self.parents_mortgage = self.parents_mortgage.to_s.gsub('/[\$,]/','').to_i
Because yes there was a problem with your syntax. Your regex needs the / wrapping, it needs to indicate that $ and , are a character group (by surrounding with []) and you need to escape the $
精彩评论