How can I add a related record from its parents partial?
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!
Change this:
<%= form_for [@review, @comment] do |f| %>
To this:
<%= form_for [review, review.comments.build] do |f| %>
Hope that helps
精彩评论