2015-04-03 50 views
0

一個很基本的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)傳遞給評論創建過程?

+0

我是新來的紅寶石,但知道數據庫相當不錯。所以,任何幫助表示讚賞。 – SwolleyBible 2015-04-03 16:45:14

回答

0

您應該在午餐模型上使用評論關係。

如果你正確地確立,那麼你可以嘗試這樣的:

<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> 
     <%- lunch.comments.each do |comment| %> 
      <p><%= comment.body %></p> 
     <% end %> 
     </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 %> 

UPD:

class CommentsController < ApplicationController 
    def create 
    lunch = Lunch.find(params[:lunch_id]) 
    lunch.comments.create!(comment_params) 
    end 

    private 

    def comment_params 
    params[:comment].permit(:body) 
    end 
end 

在這種情況下,當您發佈窗體創建評論,你應該添加lunch_id以形成參數,例如:

lunch_path(lunch_id:lunch.id)

這是主要思想。但實現可能取決於您的業務邏輯。例如,您在哪裏找到新評論表單和其他條件。

+0

所以,我的問題是建立連接。除編輯模型文件外還需要做什麼?我相信我需要以某種方式將lunch_id傳遞給評論表? – SwolleyBible 2015-04-03 16:55:52

+0

對不起,我以爲你做到了。是的,您應該在評論表中添加lunch_id。只需運行rails生成的遷移AddLunchIdToComments lunch_id:整數 然後運行rake db:migrate – 2015-04-03 17:01:41

+0

спасибо。我創建了腳手架,在評論中包含午餐ID。有關如何將午餐視圖中的lunch_id傳遞給評論的任何建議都可以創建視圖? – SwolleyBible 2015-04-03 17:21:26

0

您在描述中沒有提到它,但您需要將lunch_id列添加到評論表(如果您還沒有的話)以使關係生效。

通過定義Lunchhas_many :comments Rails將爲Lunch創建一個名爲comments的實例方法,該方法將返回關聯的註釋。

在你的觀點中,你可以做這樣的事情來顯示評論。

<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> 

     <ul>   
     <% lunch.comments.each do |comment| %> 
      <li><%= comment %></li> 
     <% end %> 
     </ul> 

     <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> 
+0

我可以創建lunch_id。但是,每條評論條目怎麼會知道與午餐_id有關?我假設午餐視圖中的某些內容需要傳遞給comments \ new.html.erb(創建註釋的地方) – SwolleyBible 2015-04-03 17:06:45

+0

正確的,當您創建註釋時,您還需要設置與其關聯的lunch_id。 – johnsorrentino 2015-04-03 17:13:08

+0

所以,我可以創建一個午餐控制器方法,創建一個實例變量傳遞給comments \ new.html.erb? – SwolleyBible 2015-04-03 17:19:20

0

您肯定需要在評論表中建立FK午餐。調整你的遷移,包括:

create_table "comments" do |t| 
    t.string :body 
    t.integer :lunch_id 
end 

然後在索引視圖中顯示的意見,通過comments每個lunch只是循環:

<td> 
    <% lunch.comments.each do |comment| %> 
    <p><%= comment.body %></p> 
    <% end %> 
</td> 
+0

知道了,我可以創建FK,但是,當用戶創建評論時,軌道如何知道將什麼午餐與其關聯?我如何將合適的午餐鑰匙傳遞給評論表? – SwolleyBible 2015-04-03 17:03:28

0

從我瞭解你創建兩個模型支架獨立。這意味着你的數據庫表沒有連接。

要實際建立關聯,您必須在評論表「lunch_id」中添加一列(這將告訴Rails評論屬於哪個午餐)。

您可以通過打開新創建的遷移文件運行後

rails g migration AddLunchIdToComment 

做到這一點,並添加行

add_column, :comments, :lunch_id, :integer 

變化函數內。

後做rake db:migrate

現在你可以用你的觀點循環內lunch.comments方法訪問午餐相關評論。這應該使你的代碼工作(不要忘記重新啓動服務器)。

相關問題