首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby不正确的链接生成redirect_to products_path(@products)生成/products.1而不是/products/1

Ruby不正确的链接生成redirect_to products_path(@products)生成/products.1而不是/products/1
EN

Stack Overflow用户
提问于 2016-01-26 02:27:47
回答 2查看 31关注 0票数 0

我正试图在我的单一产品页面上添加一个评论。但是,当我单击Submit -它带我到/products.1页面,而不是/products/1

代码语言:javascript
复制
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

代码语言:javascript
复制
 <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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-26 02:37:33

尝试redirect_to @product而不是redirect_to products_path(@product)

票数 2
EN

Stack Overflow用户

发布于 2016-01-26 02:30:37

您检查了配置下的routes.rb吗?尝试在终端中运行rake路由,您可以从那里进行调试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35006105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档