If you have a price field and someone types in e.g. "COMING SOON" the money gem tries to parse the COM as a currency and blows up. I'm not sure if this is a problem of money-rails or money..suggestions on handling are much welcome. Adding a numericality validation does not seem to help as monetize tries to intercept the field before it hits normal validations (?)
Failure/Error: product.update_attribute(:price, "COMING SOON")
undefined method `id' for "COM":String
# ./spec/models/product_spec.rb:178:in `block (3 levels) in <top (required)>'
validate :price_cents, :numericality => true
My suggestion is to not use the same price for the price amount and the flag. It's not a good idea.
Use a boolean flag or a string field to store the "Coming Soon" or any other label and display it if present.
The point is a user can type in anything random into the price field. It blows up before I even have a chance to strip it anywhere. Can't do before_validation, nothing...
I guess it can be done in the controller
I'd like to clarify this issue. We have a price field in a form. Users can type in absolutely anything. They typing in, let's say "FOO". This completely blows up money. There's no way to trap this unless we clean it in the controller which seems like the wrong place.
Is this still an issue? I'm not seeing this behavior.
no activity, closing
It's fine, but for anyone who comes across this problem, here is our solution:
PRICE_REGEX = /(?<price>[\d\s,.]+)/
return nil unless price
match = PRICE_REGEX.match(price)
clean_price = match ? match[:price] : 0
In the controller
params[:product][:price] = Reverb::Cleaners::Price.clean(params[:product][:price])