I’ve got a form for editing a model called Property. Property has two
fields, web_color and thumbnail_path, that are nil by default, and the
model has some behaviour to generate defaults for them when nil, which
is the usual case.
When the form is submitted, the text fields for these attributes come
through as empty strings, and so these attributes are changed to be
empty strings instead of nil.
I can think of some hacky ways to stop this, such as doing something
like this in the controller:
params[:property][:web_color] = nil if
But, it would be better if this behaviour lived in the model. One way
would be to set up a before_save callback like this:
self.web_color = nil if self.web_color.blank?
self.thumbnail_path = nil if self.thumbnail_path.blank?
But, this feels kind of hacky and i’d like a cleaner way. Can anyone
show me one?