I would recommend using fields_for
for this:
<%= form_for([@user, :reviews]) do |f| %>
<% @reviews.each do |review| %>
<%= fields_for review do |r| %>
<%= render "reviews/form", :r => r %>
<% end %>
<% end %>
<% end %>
To make this work, you will need to build as many review objects as you require in your controller:
def new
# you could also have this in a before_filter...
@user = User.find(params[:id])
@reviews = Array.new(3) { @user.reviews.build }
end
This would create new instances of review records for this user, which is different from new records. Instances are simply Ruby objects. Now because you've called @user.reviews.build
three times, you'll see three reviews in your view.
def create
@user = User.find(params[:id])
@reviews = Review.create(params[:reviews])
# Some more logic for validating the parameters passed in
end
This will create three new Review
objects and link them to @user
, assuming all three are valid.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…