У меня есть три модели PriceGroup, PriceGroupLine и Item. PriceGroup имеет поле -: идентификатор и: Имя товара имеет поле -: идентификатор и: Имя PriceGroupLine имеет поле -: o ID: price_group_id,: item_id,: ценаКак показать элементы модели 2 в show.html.erb модели 1
ассоциация:
PriceGroup has_many PriceGroupLines
PriceGroupLine has_many Items
PriceGroupLine belongs_to PriceGroup
I необходимо показать строки из модели PriceGroupLine на show.html.erb модели PriceGroup. Он выглядит как таблица (я не могу размещать изображения - низкая репутация (((), ПУНКТ - ЦЕНА для текущей ценовой группы. В PriceGroup может быть много PriceGroupLines. Все они должны отображаться для текущей ценовой группы.
Я новичок в Rails. Можете ли вы показать мне путь, чтобы решить мою проблему
UPDATE
мне нужно вставить в PriceGroupLine модели линий от PriceGroup шоу зрения. Как следует организовать form_for помощника, если, если мне нужно вставка:
- item_id - список элементов, организованный с использованием collection_select помощник
- price_group_id
- цена товара
Вот мой код вида show.html.erb из PriceGroup. Добавление нового элемента в ценовую группу (через линию ценовой группы) не работает. не определен метод «цена» для PriceGroup: 0x007f7201dc0aa8 ошибка
<div class="container col-md-5">
<h4>PriceGroup Info</h4>
<%= link_to "Add new price group", new_price_group_path, class: "btn btn-primary btn-xs", role: "button" %>
<%= link_to "Back", price_groups_path, class: "btn btn-primary btn-xs", role: "button" %>
<table class="table table-hover table-condensed table-bordered">
<tr>
<td><strong>Price Group name</strong></td>
<td><%= @price_group.name %></td>
</tr>
</table>
<h4>Price Group Content</h4>
<table class="table table-hover table-condensed table-bordered">
<tr>
<td><strong>Price Group Name</strong></td>
<td><strong>Price</strong></td>
<td><strong>Operations</strong></td>
</tr>
<% @price_group.price_group_lines.each do |price_group_line| %>
<tr>
<td><%= price_group_line.item.try(:name) %></td>
<td><%= price_group_line.price %></td>
<td><%= link_to "Edit", edit_price_group_line_path(price_group_line), class: "btn btn-primary btn-xs", role: "button" %> | <%= link_to "Delete", price_group_line_path(price_group_line), method: :delete, data: { confirm: "Sure?" }, class: "btn btn-primary btn-xs", role: "button" %></td>
</tr>
<% end %>
</table>
</div>
<div class="container col-md-10">
<h4>Add new item to PeiceGroup</h4>
<%= form_for @price_group, html: {class: "form-inline"} do |f| %>
<div class="form-group">
<label>Item</label>
<%= f.collection_select(:id, Item.all, :id, :name, {}, {class: "form-control"}) %>
</div>
<div class="form-group">
<label>Price</label>
<%= f.text_field :price, class: "form-control" %>
</div>
<%= f.submit "Add item", class: "btn btn-default" %>
<% end %>
</div>
Если вы делаете это, не забудьте включить 'price_group_lines' в поле для вашей переменной' @ price_group', чтобы удалить запрос БД из представления. Что-то вроде '@proce_group = PriceGroup.find (params [: id]). Include (: price_group_lines)' – Glupo
Спасибо за ответ! Почти работает!))) PriceGroupLine имеет: item_id. Как я могу показать имя элемента? –
Когда я добавил '.include (: price_group_lines)' он показывает ошибку: undefined метод 'include 'для # –