你如何从一张多面体桌子上制作一张表格? [英] How do you make a form out of a polymorophic table?
问题描述
我试图创建一条评论来评论其他评论,但都是从一篇文章中派生出来的。
对我来说尤其令人不安的是试图如何做到这一点,以便在展会后能够实现,而不是编辑或新的。这样的结构是合理的吗?
这样我可以通过 Post.comments
或 Comment.comments
等或 Comments.parent
我的模特:
#comment.rb
belongs_to:发布
belongs_to: parent,:class_name => 'Comment'
has_many:children,:class_name => 'Comment'
validates_presence_of:text
#post.rb
has_many:comments
accep_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
资源:评论
我让我的评论表具有:text
和:post_id
属性。虽然我不认为它需要:post_id
,
我的表单应该是什么样子,它应该放在哪里是吗?
这是我可怕的尝试:
- form_for @post do | f |
- f.fields_for:评论do | c |
= f.label'评论'
= f.text_area:text
= f.submit'提交'
但是这似乎是不必要的。
它在展会中,我需要指定这个帖子的URL:
- form_for @post do | f |
- f.fields_for:comments,@comment,:url => edit_post_path(@post)do | c |
= c.label'评论'
= c.text_area:text
= c.submit'提交'
但是我很困惑,因为没有错误,没有文本框!
因为我的控制器没有提到一。所以我把它添加到 def show
@comment = Comment.new
达达,现在可以运作。
完整代码
控制器:
def show
@post = Post.find(params [:id])
@comment = Comment.new
respond_to do | format |
format.html#show.html.erb
format.xml {render:xml => @post}
结束
结束
I am trying to create a comment that could comment on other comments but are all derived from a single post.
What is especially troubling for me is trying to figure out how to make it so that this can all be achieved in the post show and not its edit or new. Is this archtecturally reasonable?
That way I can access it via Post.comments
, or Comment.comments
etc. or Comments.parent
My Models:
#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
I made my comment table have a :text
and :post_id
attribute. Though I don't think it needs a :post_id
,
What should my form look like, where should it be?
Here's my awful attempt :
- form_for @post do |f|
- f.fields_for :comments do |c|
= f.label 'Comments'
= f.text_area :text
= f.submit 'Submit'
But it seems unecessary to do that.
Two Key components to finishing this was this :
Because its in the show, I need to specify the URL to which this posts :
- form_for @post do |f|
- f.fields_for :comments, @comment, :url => edit_post_path(@post) do |c|
= c.label 'Comments'
= c.text_area :text
= c.submit 'Submit'
But I was confused because where there were no errors, there was no textfield!
That because my controller didn't mention one. So I added this to def show
@comment = Comment.new
Ta da, it works now.
Full Code
Controller:
def show
@post = Post.find(params[:id])
@comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
这篇关于你如何从一张多面体桌子上制作一张表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!