I have following code in my model on my Rails 3 app. The problem is that these error messages aren't appearing in my form. I know that the error messages partial is correct because other errors from other fields are showing up. What am I doing wrong?
validates_as_geocodable :allow_nil => true do |geocode|
if geocode.nil?
model_instance.errors.add_to_base("Please specify a location")
return false
end
if geocode.precision >= :locality
model_instance.errors.add_to_base("Try to be more specific w/ your location")
return false
end
end
If you look at https://github.com/collectiveidea/acts_as_geocodable/blob/rails3/lib/acts_as_geocodable.rb you will see
if !geocode ||
(options[:precision] && geocode.precision < options[:precision]) ||
(block_given? && yield(geocode) == false)
model.errors.add :base, options[:message]
end
and if geocode.nil?
then yield(geocode)
will never be invoked => your block will not be invoked too. Don't know about second condition, but I think you should check it too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.