【问题标题】:How to specify correct url/path to a parent object only by its ID?如何仅通过其 ID 指定父对象的正确 url/路径?
【发布时间】:2019-09-19 08:24:47
【问题描述】:

routes.rb

  resources :customers do
    resources :orders
  end

  resources :orders, :only => [:index]

我有意分隔订单索引以列出所有客户的所有订单

我能够通过以下方式访问父对象的特定字段(我相信我必须将此计算放在帮助程序中):

<%= Customer.find(order.customer_id).clinic_name %>

但不知道如何指定customer_path 的正确路径,以便将其带到其父对象的显示页面。 现在,我在下面标记为 _what_to_put_here_,我正在努力弄清楚?

<% @orders.each do |order| %>
  <tr>
    <td><%= order.doctor_name %></td>
    <td><%= order.patient_name %></td>
    <td><%= link_to <%= Customer.find(order.customer_id).clinic_name, 
                        customer_path(_what_to_put_here_) %></td>
    <td><%= order.note %></td>
  </tr>
<% end %>

【问题讨论】:

    标签: ruby routes nested relational-database ruby-on-rails-6


    【解决方案1】:

    试试这个方法:

    <% @orders.each do |order| %>
      <tr>
        <td><%= order.doctor_name %></td>
        <td><%= order.patient_name %></td>
        <td><%= link_to order.customer.clinic_name, customer_path(order.customer) %></td>
        <td><%= order.note %></td>
      </tr>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2020-09-21
      • 2014-06-03
      相关资源
      最近更新 更多