【问题标题】:Nested routes pointing to wrong routes [duplicate]指向错误路线的嵌套路线[重复]
【发布时间】:2017-10-07 06:56:31
【问题描述】:

在我的路线文件中

  resources :shops do
    resources :products
  end

我有一个产品列表,例如

        <% @products.each do |p| %>
        <div class="item">
            <p class="text-center"><%= link_to p.name, shop_product_path(p) %></p>
            <p class="text-center"><strike>Rs.250</strike>&nbsp;<strong>Rs.<%= p.price %></strong></p>
            <center><%= link_to "Order now", new_product_order_path(p), :class => "btn btn-primary" %></center>
        </div>
    <% end %>

但是这个链接在这里

<p class="text-center"><%= link_to p.name, shop_product_path(p) %></p>

它指向

http://localhost:5000/shops/6/products/1

但这应该是这样的

http://localhost:5000/shops/1/products/6

这里有什么错误?

【问题讨论】:

  • p.idp.shop_id 是什么?
  • 商品ID为6,店铺ID为1
  • 我认为您需要将两个 id 都传递给路由助手

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5


【解决方案1】:

我们需要传递两个对象

<p class="text-center"><%= link_to p.name, shop_product_path(@shop, p) %></p>

p 是循环中的@product。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多