0

我正在尝试在我的应用程序中创建评论,但是当我在控制器中执行以下操作时:

def create
    @product = Product.find(params[:product_id])
    @review = @product.reviews.create(params[:review])

   redirect_to product_path(product)
  end

我明白了Couldn't find Product without an ID。我填写了表格,当它执行以下行时@product = Product.find(params[:product_id]),它似乎无法找到产品

路线

 resources :products do
    resource :reviews
  end

产品/show.html.erb

 <%= form_for(@product.reviews.build) do |f| %>
   <div class="field">
     <%= f.label :title %>
     <br/>
     <%= f.text_field :title %>
   </div>
   <div class="field">
     <%= f.label :author %>
     <br/>
     <%= f.text_field :author %>
   </div>
   <div class="field">
     <%= f.label :content %>
     <br/>
     <%= f.text_area :content %>
   </div>
   <div class="field">
     <%= f.label :rating %>
     <br/>
     <%= f.number_field :rating %>
   </div
   <div class="actions">
     <%= f.submit %>
   </div>
  <% end %>
4

1 回答 1

-1

将您的 form_for 更改为

form_for [@product, @product.reviews.build] do |f|

这会将操作更改/products/@product.id/reviews为匹配您的嵌套路由

于 2013-03-13T03:26:42.480 回答