El problema (al menos para mí) es que mi f.select :whatever_id
estaba buscando en el objeto object.errors
para una clave de validación :whatever_id
cuando mi era en realidad en :whatever
, no :whatever_id
.
trabajé en torno a este molesto problema cambiando
object.errors.on(@method_name)
a
object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, ''))
Aquí está el diff (contra rieles 2.3.4):
diff --git a/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb b/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb
index 541899e..5d5b27e 100644
--- a/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb
+++ b/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb
@@ -247,7 +247,7 @@ module ActionView
alias_method :tag_without_error_wrapping, :tag
def tag(name, options)
if object.respond_to?(:errors) && object.errors.respond_to?(:on)
- error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name))
+ error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, '')))
else
tag_without_error_wrapping(name, options)
end
@@ -256,7 +256,7 @@ module ActionView
alias_method :content_tag_without_error_wrapping, :content_tag
def content_tag(name, value, options)
if object.respond_to?(:errors) && object.errors.respond_to?(:on)
- error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name))
+ error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, '')))
else
content_tag_without_error_wrapping(name, value, options)
end
Hi. ¡Gracias por la respuesta! Encontré eso también, pero él realmente no especifica si y cómo lo hizo funcionar. Hay un enlace al wiki de Rails, pero está roto. Me gustaría evitar la mayor cantidad posible de agregar una prueba en 'error_message_on' para cada selección porque eso realmente complicaría mis puntos de vista. – andi
Especifica cómo hacerlo funcionar aquí: "La única solución es cambiar la validación en el modelo working_time de project to project_id". Es decir, en la clase de modelo, cambie "validates_presence_of: project" por "validates_presence_of: project_id" –