涉及的一些代码:
Post Model:
ruby
class Post < ActiveRecord::Base
validates :title, :presence => true, :uniqueness => true
validates :content, :presence => true
has_many :comments
end
class Post < ActiveRecord::Base validates :title, :presence => true, :uniqueness => true validates :content, :presence => true has_many :comments end
Comment Model:
ruby
class Comment < ActiveRecord::Base
belongs_to :post
end
class Comment < ActiveRecord::Base belongs_to :post end
Comments Controller:
ruby
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:comment])
redirect_to @post if @comment.save
end
end
class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.new(params[:comment]) redirect_to @post if @comment.save end end
