【发布时间】:2010-10-14 22:40:31
【问题描述】:
我正在尝试创建一个可以评论其他 cmets 但均来自单个帖子的评论。
对我来说特别麻烦的是试图弄清楚如何制作它,以便这一切都可以在后期展示中实现,而不是它的编辑或新的。这在架构上合理吗?
这样我可以通过Post.comments或Comment.comments等或Comments.parent访问它
我的模特:
#comment.rb
belongs_to :post
belongs_to :parent, :class_name => 'Comment'
has_many :children, :class_name => 'Comment'
validates_presence_of :text
#post.rb
has_many :comments
accepts_nested_attributes_for :comments
posts_controller
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
routes.rb
resource :comments
我让我的评论表有一个:text 和:post_id 属性。虽然我认为它不需要:post_id,
我的表单应该是什么样子,应该在哪里?
这是我糟糕的尝试:
- form_for @post do |f|
- f.fields_for :comments do |c|
= f.label 'Comments'
= f.text_area :text
= f.submit 'Submit'
但似乎没有必要这样做。
【问题讨论】:
标签: ruby-on-rails forms nested nested-forms