我正试图在我的单一产品页面上添加一个评论。但是,当我单击Submit -它带我到/products.1页面,而不是/products/1
class CommentsController < ApplicationController
def create
@product = Product.find(params[:product_id])
@comment = @product.comments.new(comment_params)
@comment.user = current_user
@comment.save
redirect_to products_path(@product)
end
def destroy
end
private
def comment_params
params.require(:comment).permit(:user_id, :body, :rating)
end
end和comment.html.erb
<div class="row">
<div class="col-sm-6">
<% if signed_in? %>
<h4>Add a review:</h4>
<%= form_for([@product, @product.comments.build]) do |f| %>
<p>
<%= f.label :body, "Comment" %><br>
<%= f.text_area :body, class: "form-control" %>
</p>
<p>
<%= f.label :rating %><br>
<%= f.text_field :rating, class: "rating form-control" %>
</p>
<p>
<%= f.submit "Submit", class: "btn" %>
</p>
<% end %>
<% end %>
</div>
</div>发布于 2016-01-26 02:37:33
尝试redirect_to @product而不是redirect_to products_path(@product)。
发布于 2016-01-26 02:30:37
您检查了配置下的routes.rb吗?尝试在终端中运行rake路由,您可以从那里进行调试。
https://stackoverflow.com/questions/35006105
复制相似问题