У меня есть две модели Item
и Prom
. Элемент имеет столбец выпускной , а выпускной имеет столбец выпускной . Поэтому я хочу, чтобы элемент создавался только в том случае, если в базе данных существует выпускной номер.
example
Prom.exist?(promname: :prom) == true #create
else
"i dont have that prom"
class Item < ApplicationRecord
belongs_to :prom
end
class Prom < ApplicationRecord
end
и представление с созданием элемента с помощью
<%= form_with(model: @item, local: true, html: { autocomplete: "off" }) do |form| %>
<% if @item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% @item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<table class="content-table">
<thead>
<tr>
<th>Item Name</th>
<th>Prom Name</th>
<th>Prom Code</th>
<th>Price</th>
<th>M.M</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<tr>
<% @items.each do |item| %>
<tr>
<td><%= item.itemName %></td>
<td><%= item.prom %></td>
<td><%= item.promCode %></td>
<td><%= item.price %></td>
<td><%= item.monadaMe %></td>
<td><%= link_to 'Edit', edit_item_path(item), class: 'action_button' %></td>
<td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' }, class: 'action_button' %></td>
</tr>
<% end %>
</tbody>
</table>
<table class="insert-to-items">
<thead>
<tr>
<th>Item Name</th>
<th>Prom Name</th>
<th>Prom Code</th>
<th>Base Code</th>
<th>Description</th>
<th>Price</th>
<th>ΦΠΑ %</th>
<th>Οικογενεια</th>
<th>M.M</th>
</tr>
</tr>
<tbody>
<tr>
<td><input size="13" <%= form.text_field :itemName %></td>
<td><input id="myInput" size="13" <%= form.text_field :prom %></td>
<td><input size="13" <%= form.text_field :promCode %></td>
<td><input size="13" <%= form.text_field :baseCode %></td>
<td><input size="13" <%= form.text_field :desc %></td>
<td><input type="number" <%= form.number_field :price, step: '0.001' %></td>
<td><input type="number" <%= form.number_field :fpa %></td>
<td><input size="13" <%= form.text_field :familys %></td>
<td><input size="13" <%= form.text_field :monadaMe %></td>
</tr>
</tbody>
</thead>
</table>
<script>
var promList = <%= raw @autoComplete.to_json %>;
</script>
<div class="actions">
<center><%= form.submit "Save", class: 'button-items' %></center>
<%= javascript_pack_tag 'find_me' %>
</div>
<% end %>
Пример: Prom.promname = «Компания» существует в базе данных, и когда я захотел создать Item.prom.promname = «Компания», он ее создаст. Но если он отсутствует, он не собирается его создавать.
Я новичок в Ruby на Rails, и если вам нужна дополнительная информация, связанная с вопросом, не стесняйтесь спрашивать, заранее спасибо.