Ruby on rails Ruby不正确的链接生成将\u重定向到products\u路径(@product)generate/products.1,而不是/products/1
我试图在我的单一产品页面上添加评论。但是当我单击Submit时,它会将我带到/products.1页面,而不是/products/1Ruby on rails Ruby不正确的链接生成将\u重定向到products\u路径(@product)generate/products.1,而不是/products/1,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试图在我的单一产品页面上添加评论。但是当我单击Submit时,它会将我带到/products.1页面,而不是/products/1 class CommentsController < ApplicationController def create @product = Product.find(params[:product_id]) @comment = @product.comments.new(comment_params) @comment.user
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
class CommentsController
以及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>
添加评论:
您是否检查了配置下的routes.rb?尝试在终端中运行rake路由,您可以从那里进行调试。您是否在配置下检查了routes.rb?尝试在终端中运行rake路由,您可以从那里进行调试。尝试重定向到@product
而不是重定向到products\u路径(@product)
尝试重定向到@product
而不是重定向到products\u路径(@product)
是,我看到routes.rb,正确的链接是products/1我不明白是什么让重定向到products\u path(@product)生成产品。1如果你看一下Michael Hart提供的railstutorial.org,在本节中,他解释了只使用对象名而不使用对象路径(@object)的概念。这可能是你的问题。是的,我看到routes.rb,正确的链接是products/1我不明白是什么让重定向到products路径(@product)生成产品。1如果你看一下Michael Hart提供的railstutorial.org,在这一节中,他解释了只使用对象名而不是对象路径(@object)的概念。那可能是你的问题。谢谢!我只是自己发现的)是的,就是这样谢谢!我只是自己发现的是的,它是这样工作的