1
我有article
和comment
模型。如何顯示關聯模型中的錯誤?
我想這樣寫:
= form_for ([@article, @article.comments.build]) do |f|
- if @article.comments.errors.any?
%h4 Errors
%ul
- @article.comments.errors.full_message do |message|
%li= message
,但我得到的錯誤:
undefined method `errors' for Comment::ActiveRecord_Associations_CollectionProxy:0x9a4a020
Article
有許多意見和comment
屬於文章。
我想爲我的評論顯示驗證錯誤。
編輯: 我comment
型號:
class Comment < ActiveRecord::Base
belongs_to :article
validates :author, presence: true, length: { minimum: 3 }
validates :body, presence: true, length: { minimum: 5 }
end
'@ article.comments.first.'? –
現在沒有錯誤。但驗證錯誤不會顯示時應該(模型中我有存在驗證等)。 – Jensky
顯示您在模型中寫入的驗證 –