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
显示您在模型中写入的验证 –