How to pass parameter for validation/processing in the RUBY view

Asked

Viewed 640 times

0

I’m in deep doubt, I’ll take tips if you see a better option to do what I’m looking for. I have a layout that links to two items in the same view>>

<ul class="nav nav-second-level">
  <li>
   <%= link_to backoffice_pedidos_path do %>
     Abertos
   <% end %>
  </li>
  <li>
    <%= link_to backoffice_pedidos_path do %>
      Finalizados
    <% end %>
   </li>
 </ul>

I want to treat in the view the contents of my select, if it clicks OPEN, it loads the index with a partial _open and if it clicks Finished it loads the index but with another partial _finished. Is there any way to pass some parameter in the link_to so that in the view I can treat it to send to the correct partial? Or any hint as to how it might look?

index>>

                    <thead>
                    <tr>
                        <th>Pedido</th>
                        <th>Status</th>
                        <th>Data/Hora </th>
                        <th>Produtos </th>                    
                        <th> </th>
                    </tr>
                </thead>
                <tbody> 
                  <% if ??
                  <%= render partial: "backoffice/pedidos/abertos" %>
                  else ??
                  <%= render partial: "backoffice/pedidos/finalizados" %>
                  <% end %>
                </tbody>

open partial>>

           <% @pedidos_aguardando.each do |pedido| %>
        <tr> 
          <td><%=pedido.id%></td>
          <td><%=pedido.status%></td>
          <td><%=pedido.created_at%></td>
          <!--<th><%=pedido.produtos.first.produto %></th>-->
          <td><%=pedido.produtos.pluck (:produto)%></td>
          <td width="50px">
              <%= link_to edit_backoffice_pedido_path(pedido), class:"btn btn-primary btn-circle" do %>
                <i class="fa fa-edit"></i>
              <% end %>
          </td>
        </tr>
      <% end %>

Partials closed

           <% @pedidos_finalizados.each do |pedido| %>
        <tr> 
          <td><%=pedido.id%></td>
          <td><%=pedido.status%></td>
          <td><%=pedido.created_at%></td>
          <!--<th><%=pedido.produtos.first.produto %></th>-->
          <td><%=pedido.produtos.pluck (:produto)%></td>
          <td width="50px">
              <%= link_to edit_backoffice_pedido_path(pedido), class:"btn btn-primary btn-circle" do %>
                <i class="fa fa-edit"></i>
              <% end %>
          </td>
        </tr>
      <% end %>

Controller

def index
@pedidos_aguardando = Pedido.waiting
@pedidos_finalizados = Pedido.ok
 end

Model

class Pedido < ActiveRecord::Base
has_many :produtos

scope :waiting, -> { where(status: 1) }
scope :ok, -> { where(status: 2) }
end

Thank you!!

1 answer

0


You put the parameter in the link:

link_to backoffice_pedidos_path(type: 'open')

this will give you in the controller access to a params[:type]

put this in a variable, remembering to treat the case of the first load, where this parameter will be empty ex:

@tipo = params[:tipo] || 'abertos'

Use the name of the partitals in the parameters, so in the view you do:

<%= render partial: "backoffice/pedidos/#{@tipo}" %>
  • Thank you very much, I had done it differently, so it greatly simplified my code, I had passed a FLAG parameter, and I was dealing with IF in the view. That way, treating in the controller is much better!! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.