Error rendering a partial of a controller from another controller

0

I am trying to render a table of records for a Orders controller in another controller called Dashboard . I've read a lot about doing it with:

<%= render 'orders/index'%>

And it throws the following error:

The current code of my view is:

<div class="container-fluid">
<!-- Primera fila de información -->
<div class="row align-content-center">
    <div class="col col-sm-6 border border-dark">
        <h3>Primer Espacio</h3>

        <!-- <%= render 'orders/index' %> -->
        <%=render 'layouts/tables' %>

    </div>
    <div class="col col-sm-6 border border-dark">
        <h3> Segundo Espacio</h3>
        <%=render 'layouts/tables' %>
    </div>
</div>

I have commented on the erb code to avoid the failure that I am trying to solve. The partial is that it generates by default the Scaffold command in the index.html.erb view p>

<p id="notice"><%= notice %></p>

<h1>Orders</h1>

<table class="table table-sm">
  <thead>
    <tr>
      <th>Fecha pedido</th>
      <th>Valorpedido</th>
      <th>Provider</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @orders.each do |order| %>
      <tr>
        <td><%= order.fecha_pedido %></td>
        <td><%= order.valorPedido %></td>
        <td><%= order.provider %></td>
        <td><%= link_to 'Show', order %></td>
        <td><%= link_to 'Edit', edit_order_path(order) %></td>
        <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>


I have read several methods to do it such as:

<%= render :partial 'orders/order', local:{order:@order}%>

<%= render "orders/index", orders: @orders%>

I think the problem lies in not knowing how to pass the collection of objects

orders

My question is: Can someone help me about how to use the helper correctly?

<%=render%>

The result I would like to achieve is this:

    
asked by Diego Ordoñez Morales 27.11.2017 в 18:46
source

1 answer

0

The problem is that when you try to render the partial you are passing a local variable orders instead of an instance variable @orders , to solve it, you will need to use the same type of variable on both sides.

Actually what happens is that you are breaking a Rails convention that is not so explicit: the views are divided into vistas and _partials , the views correspond to an action of a controller and uses instance variables (like orders/index ) and the partials use local variables and do not correspond to any action since they are used from different actions.

What you are doing is trying to use a view as partial and although rails lets you do it, in the end it gets complicated. What you should do is extract a partial with what you want to share and reuse that partial.

The result would be the following:

orders / index.html.erb

<p id="notice"><%= notice %></p>

<h1>Orders</h1>

<%= render 'orders_table', orders: @orders %>

orders / _table.html.erb

<table class="table table-sm">
  <thead>
    <tr>
      <th>Fecha pedido</th>
      <th>Valorpedido</th>
      <th>Provider</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% orders.each do |order| %>
      <tr>
        <td><%= order.fecha_pedido %></td>
        <td><%= order.valorPedido %></td>
        <td><%= order.provider %></td>
        <td><%= link_to 'Show', order %></td>
        <td><%= link_to 'Edit', edit_order_path(order) %></td>
        <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</ta

ble >

And in the dashboard you use:

dashboard / index.html.erb

<%= render 'orders_table', orders: first_orders %>
    
answered by 22.01.2018 в 20:35