0

我需要您的帮助以了解如何在表单下方的表格中显示用户在表单中输入的内容。有一个模型(输入),我希望只显示最近添加到数据库表中的内容......我已经有了表格,表格显示了包含太多行的模型索引,我希望我能说清楚。我知道会涉及到一些 jQuery 和 Ajax。新的输入记录将被添加到数据库的表中,但我只想显示用户输入的内容。也许已经有针对此或其他解决方案的 Railscast,我只是不知道如何搜索或询问。谢谢你。这是我已经拥有的表格:

<%= form_with(model: input, id: "new-input", local: false) do |form| %>
  <% if input.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(input.errors.count, "error") %> prohibited this input from being saved:</h2>

      <ul>
        <% input.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
   
  <div style="padding-top: 50px;padding-bottom: 30px;">
    <table class="show-table">
      <tr>
        <td>Proveedor:</td>
        <td colspan="3"><%= form.collection_select :supplier_id, @suppliers, :id, :name, { :prompt => "Proveedor sugerido"}%></td>
      </tr>
      <tr>
        <td>Factura:</td>
        <td><%= form.text_field :invoice %></td>
        <td>Fecha:</td>
        <td><%= form.date_field :input_date %></td>
      </tr>
    </table>
    <div class="void-space">
      <br>
    </div>
    <table class="index-table">
      <thead>
        <tr>
          <td>Producto</td>
          <td>Precio Unit.</td>
          <td>Cantidad</td>
          <td>Costo</td>
          <td>Ubicación</td>
          <td>Acción</td>
        </tr>
      </thead>
      <tbody>
        <%  %>
        <tr>
          <td><%= form.collection_select :product_id, @products, :id, :name, { :prompt => "Producto"}%></td>
          <td><%= form.text_field :price %></td>
          <td><%= form.text_field :quantity %></td>
          <td>costo</td>
          <td><%= form.text_field :location %></td>
          <% form.hidden_field :user_id, value: current_user.id %>
          <td><%= form.submit "Guardar", class: "btn success" %></td>
        </tr>
      </tbody>
    </table>
  </div>

<% end %>

这是从 index.html.erb 呈现的,它工作正常,但我不想看到所有输入的表记录,只是新的

4

2 回答 2

0

我想您将其存储user_id在 Product 模型上。在这种情况下,您可以在 Product 模型上添加范围

scope :created_by, ->(user_id) { where(user_id: user_id) }

并更改您的表以使用范围集合,因此它最终会像

<td><%= form.collection_select :product_id, @products.created_by(current_user.id), :id, :name, { :prompt => "Producto"}%></td>

于 2021-06-13T05:25:43.487 回答
0

So I found the solution, it may be of interest for someone, so here it is: It involves a new action in the controller to handle this situation where I want to show the table listing the records being entered ... Create a route and a new action in inputs_controller.rb :

# GET /list_inputs
  def list_inputs
    @input_list = params[:input] || []
    new_input
  end

'new_input' is a method to iniialize all the instance variables also used in actions 'new' and 'create':

def new_input
  if action_name == 'create'
    @input = Input.new(input_params)
  else
    @input = Input.new
  end
  @suppliers = Supplier.all
  @products = Product.all
  @user = current_user
end

A view is defined then app/views/inputs/list_inputs.html.erb

<h1>Registro de entradas al almacén</h1>

<%= render 'form', input: @input %>

<table class="index-table">
  <tbody id='input-list'>
    <% @input_list.each do |input| %>
      <%= render 'inputs', input: input %>
    <% end %>
  </tbody>
</table>

<br>

<%= content_for :footer do %>
  <p id="notice"><%= notice %></p>
<%  end %>

A partial for 'inputs' (app/views/inputs/_input.html.erb):

<tr>
  <td><%= input.product.name %></td>
  <td><%= input.quantity %></td>
  <td><%= input.user.name %></td>
  <td><%= input.location %></td>
  <td class="botones"><%= link_to 'Destroy', input, class: "btn info", method: :delete, data: { confirm: '¿Confirma?' } %></td>
</tr>

And the javaScript to

respond_to do |format|

in the 'inputs_controller#create' action, which must be located in app/views/inputs/create.js.erb

// Render input
var body = document.getElementById("input-list");
body.innerHTML = "<%= j render @input %>" + body.innerHTML;

//Clear and Focus input form
var input = document.getElementById("input_product_id");
input.value = '';
input.focus();

This code produces this effect: input form and table

I am aware that this may not be the most effective solution but it works, if you are kind enough to comment I'll be thankful.

于 2021-06-13T20:22:55.023 回答