我有一个名为属性的模型和一个嵌套模型 PropertyReviews。我可以使用 property 和 property_reviews 呈现单个表单。一个属性可以有多个评论,所以当我想添加另一个评论时,我不想为属性呈现表单,而只是为了评论。这是我的代码。
#for fresh property (with review)
def new
@property = Property.new
@property.property_reviews.build
end
下面是属性模型:
class Property < ActiveRecord::Base
has_many :property_reviews, :dependent => :destroy
accepts_nested_attributes_for :property_reviews
end
以下是视图和部分内容:
app/views/property/new.html.erb
<%= render :partial => 'form' %>
app/views/property/_from.html.erb
<%= form_for @property, :html => {multipart: true} do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :address, "Complete Address" %>
<%= f.text_area :address, rows: 3 %>
<%= f.fields_for :property_reviews do |review| %>
<%= render "review_fields", r: review %>
<% end %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
app/views/property/_review_fields.html.erb
<p>
<%= r.label :comments %>
<%= r.text_area :comments, rows: 5 %>
..
..
</p>
到现在为止一切都还好,因为我渲染了新的内容来创建一个带有审查的新属性。
现在我只想添加一个评论,因为属性地址是固定的,它的控制器代码如下:
# add another review to existing property
def add_new_review
@property = Property.find(params[:id])
@review = @property.property_reviews.build
end
视图:app/views/property/add_new_review.html.erb
<b><%= @property.address %></b>
<%= form_for @review do |r| %>
<%= render "review_fields" %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
当我尝试渲染 add_new_review 页面时,出现以下错误:
undefined method `property_reviews_path' for #<#<Class:0xa2ac3a8>:0xa5e487c>
在线的:
<%= form_for @review do |r| %>
这是我的第一个 Rails 项目,请告诉我我做错了什么?