开发者

Customizing ActsAsGeocodables Error Messages

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 
开发者_JS百科  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜