一個很基本的RoR問題,我似乎無法找到一個在線答案:如何將評論表連接到另一個表格?
我有兩個獨立的表,用腳手架創建。 午餐和評論。在腳手架期間沒有建立關係。如果需要,我可以重做評論腳手架。
我需要能夠在午餐索引視圖中接收並顯示每個午餐的相關評論。有人能告訴我該怎麼做嗎?
我編輯的模型\ comment.rb到:
class Comment < ActiveRecord::Base
belongs_to :lunch
end
我編輯的模型\ lunch.rb到:
class Lunch < ActiveRecord::Base
has_many :comments, dependent: :destroy
end
在午餐查看我有一個循環,列出了所有午餐列:
<tbody>
<% @lunches.each do |lunch| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even')%>">
<td><%= lunch.company %></td>
<td><%= lunch.person %></td>
<td><%= lunch.email_submit_lunch %></td>
<td><%= lunch.company_contact %></td>
<td class="list_description"><%= truncate(strip_tags(lunch.description), length: 40) %></td>
<td><%= lunch.date %></td>
<td><%= lunch.price %></td>
<td class="list_actions"><%= link_to 'Show', lunch %></td>
<td class="list_actions"><%= link_to 'Edit', edit_lunch_path(lunch), data: { confirm: 'Are you sure?' } %></td>
<td class="list_actions"><%= link_to 'Destroy', lunch, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
**編輯:我已經重做了包含lunch_id外鍵的評論表。我如何將午餐視圖中的外鍵(lunch_id)傳遞給評論創建過程?
我是新來的紅寶石,但知道數據庫相當不錯。所以,任何幫助表示讚賞。 – SwolleyBible 2015-04-03 16:45:14