如何从其父部分添加相关记录?
我有一个评论和评论表,其中一个评论可以有很多评论。
如何在评论部分中保留添加新评论表单?
我现在所拥有的给出了“NilClass:Class 的未定义方法‘model_name’”错误。
_review.html.erb
<div class="review">
<div class="review_content">
<h2 class="review_partial_title"><%= review.title %></h2>
<p class="review_body"><%= review.body %></p>
</div>
<div class="clearall"></div>
<div class="comments_container">
<%= render :partial => 'comments/comment', :collection => review.comments %>
</div>
<div class="add_comment_container">
<%= form_for [@review, @comment] do |f| %>
<p>body: <br>
<%= f.text_field :body %></p>
<%= submit_tag %>
<% end %>
</div>
<div class="clearall"></div>
</div>
review.rb
class Review < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
end
routes
resources :reviews do
resources :comments
end
感谢您的帮助,非常感谢!
I have a table of reviews and comments, where a review can have many comments.
How can I have the add new comment form held inside the review partial?
What I have at the moment gives a "undefined method `model_name' for NilClass:Class" error.
_review.html.erb
<div class="review">
<div class="review_content">
<h2 class="review_partial_title"><%= review.title %></h2>
<p class="review_body"><%= review.body %></p>
</div>
<div class="clearall"></div>
<div class="comments_container">
<%= render :partial => 'comments/comment', :collection => review.comments %>
</div>
<div class="add_comment_container">
<%= form_for [@review, @comment] do |f| %>
<p>body: <br>
<%= f.text_field :body %></p>
<%= submit_tag %>
<% end %>
</div>
<div class="clearall"></div>
</div>
review.rb
class Review < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
end
routes
resources :reviews do
resources :comments
end
Thanks for any help its much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改此:
改为:
希望有帮助
Change this:
To this:
Hope that helps