简体   繁体   English

rails nested_attribute

[英]rails nested_attribute

I want to create a comment form at the bottom of a blog entry. 我想在博客条目的底部创建评论表单。 The problem is that it doesn't save the blog_id so while the comment is created, it isn't assigned a blog_id. 问题在于它不会保存blog_id,因此在创建注释时不会为其分配blog_id。 I thought that by having this line: 我以为有这样一行:

@new_comment = @blog.comments.build(params[:comment])

that it would work but it doesnt. 它会工作,但不会。 This is what I have: 这就是我所拥有的:

blog#show 博客#显示

def show
 @blog = Blog.find(params[:id])
 @new_comment = @blog.comments.build(params[:comment])
end

blog#form 博客#form

...
<%= semantic_form_for @new_comment do |f| %>
  <%= f.input :name %>
  <%= f.input :content, :label => "Comment", :input_html => { :rows => 6, :class => "xxlarge" } %>
<div class="pull-right"><%= f.commit_button :button_html => {:class => "primary"}, :label => "Submit" %></div>
<% end %>

blog.rb blog.rb

 accepts_nested_attributes_for :comments

comments controller 评论控制器

  def create
@comment = @blog.comments.build(params[:comment])
if @comment.save
  redirect_back_or show_blog_path(@blog)
else
  redirect_to show_blog_path(@blog)
end
end

EDIT: 编辑:

I nested the form inside the @blog by doing: 我这样做是将表单嵌套在@blog中:

<%= semantic_form_for @blog do |f| %>
<%= f.semantic_fields_for :comments do |ff| %>
  <%= ff.input :name %>
   <%= ff.input :content, :label => "Comment", :input_html => { :rows => 6, :class => "xxlarge" } %>
<% end %>    
<div class="pull-right"><%= f.commit_button :button_html => {:class => "primary"}, :label => "Submit" %></div>
<% end %>

and changed the blogs#show to: 并将Blog#show更改为:

@blog = Blog.find(params[:id])
@blog.comments.build
@comments = Comment.where(:blog_id => @blog.id)

Everything works fine, but now after I submit the comment, the comment shows up twice. 一切正常,但是现在我提交评论后,评论出现了两次。 It shows up in the comments section where it is supposed to and it also shows up as a prepopulated comments form on top of a new comments form. 它显示在应有的注释部分,并且还显示为新注释表单顶部的预填充注释表单。 So you see the content of the blog, the prepopulated comment form that I just submitted, a new comments form, and the comment in the display comments section. 因此,您会看到博客的内容,我刚刚提交的预先填充的评论表单,新的评论表单以及“显示评论”部分中的评论。

I don't know what that it. 我不知道那是什么

You are not referencing the parent object in your form. 您没有在表单中引用父对象。 Yes, you have initialised @new_comment which references the current @blog , but the comment form does not specify this. 是的,您已经初始化了@new_comment ,它引用了当前的@blog ,但注释表单未指定此内容。 If you inspect the params passed to create action of comments controller, you are most likely to find no bold_id being passed. 如果检查为创建注释控制器动作而传递的参数,则最有可能找不到要传递的bold_id

Try doing 尝试做

f.hidden :blog 

in the comment form. 在评论表格中。

Or, if you do not wish to have a hidden field as such, then you can have a nested form as 或者,如果您不希望有这样的隐藏字段,则可以有一个嵌套的表单,例如

semantic_form_for [@blog, @new_comment] do |f|

But, this requires your routes file to have paths for comments to be nested under blogs, something as(lets take only the create action for now) 但是,这需要您的路由文件具有在博客下嵌套注释的路径,就像(现在只执行创建操作)

resources :blogs do
   resources :comments, :only => [:create]
end

This is required as the form's post is directed to blog_comments_path. 这是必需的,因为表单的帖子直接指向blog_comments_path。

Hope this helps. 希望这可以帮助。

EDIT : This is how I suggest implementing it (ymmv, so, please be clear if it satisfies your needs) - 编辑 :这是我建议实施的方式(ymmv,因此,请明确它是否满足您的需求)-

blogs#show as : blogs#show为:

@blog = Blog.find(params[:id])
@now_comment = @blog.comments.build 
@comments = @blog.comments # existing comments u might wanna show below the post

The form as : 形式为:

= semantic_form_for [@blog, @new_comment] do |f|
   # input for name and content

Routes - nested as shown above 路线-如上图所示嵌套

comments#create as : (blog_id is passed as params too. So, you can find the blog and build a comment based on comment params for that blog and then save it) comments#create as:(blog_id也作为参数传递。因此,您可以找到博客并根据该博客的评论参数构建评论,然后将其保存)

@blog = Blog.find(params[:blog_id])
@comment = @blog.comments.new(params[:comment])
@comment.save

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM