Vấn đề (cho tôi ít nhất) là f.select :whatever_id
tôi đang tìm kiếm trong đối tượng object.errors
cho một chìa khóa của :whatever_id
khi xác nhận tôi đã thực sự vào :whatever
, không :whatever_id
.
tôi làm việc xung quanh vấn đề này gây phiền nhiễu bằng cách thay đổi
object.errors.on(@method_name)
để
object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, ''))
Đây là diff (chống Rails 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
Nguồn
2009-12-17 07:18:59
Xin chào. Cảm ơn vi đa trả lơi! Tôi cũng tìm thấy điều đó, nhưng anh ấy không thực sự xác định liệu anh ta có làm việc hay không. Có một liên kết đến wiki đường ray, nhưng nó bị hỏng. Tôi muốn tránh càng nhiều càng tốt thêm một thử nghiệm trên 'error_message_on' cho mỗi lựa chọn bởi vì điều đó thực sự sẽ làm phức tạp quan điểm của tôi. – andi
Anh ta chỉ định cách làm cho nó hoạt động ở đây: "Giải pháp duy nhất là thay đổi xác thực trong mô hình working_time từ project thành project_id". Đó là, trong lớp mô hình, thay đổi "validates_presence_of: project" thành "validates_presence_of: project_id" –