-
@article.errors.any?
检查是否有错误,如果有错误,@article.errors.full_messages
显示错误
-
new
动作中加入@article = Article.new
的原因是,如果不这么做,在视图中@article
的值就是nil
,调用@article.errors.any?
时会发生错误。
-
$ rails generate model Comment commenter:string body:text article:references
article references 会在comment.rb 建立benlongs_to
class Comment < ActiveRecord::Basebelongs_to :articleend
-
我们要编辑
app/models/article.rb
文件,加入这层关系的另一端:
class
Article < ActiveRecord::Base
has_many :comments
validates
:title
, presence:
true
,
length: { minimum:
5
}
end
|