2017-10-20 161 views
2

所以我有一個食譜應用程序,我正在努力,我不明白爲什麼我沒有得到顯示的東西。我將Recipe作爲連接表使用,爲了簡單起見,我只將它與其他三個表一起包括在內。爲了應用程序的目的,我唯一想要顯示的是與每個模型關聯的名稱。Ruby on Rails中連接表的正確關聯是什麼?

我有以下型號: 蔬菜 蛋白 澱粉 配方

配方充當連接表,並具有:

belongs_to :vegetable 
belongs_to :protein 
belongs_to :starch 
accepts_nested_attributes_for :vegetable, :protein, :starch 

其他每個模型有:

has_one :recipe 

所以我認爲的觀點是一個簡單的迭代

<table> 
<thead> 
<tr> 
<th> Vegetable Name </th> 
<th> Protein Name </th> 
</tr> 
</thead> 
<% @recipes.each do |recipes| %> 
<tr> 
<td> <%= recipe.vegetable.name %> <td> 
<td> <%= recipe.protein.name %> <td> 
</tr> 

然而沒有任何東西超出標題。我已經做了各種各樣的事情,例如將食譜改爲食譜,將食譜作爲參數傳遞給每列的末尾。我確信,我的食譜控制器有以下幾點:

def recipe_params 
params.require(:recipe).permit(:vegetable_id, :protein_id, :starch_id) 
end 

我錯了,我協會,這就是爲什麼它沒有呈現什麼?

+0

'@ recipes'的價值是什麼?你能展示初始化它的代碼嗎? – hoffm

+0

您確定在數據庫中有一些數據可以顯示嗎? – kolas

回答

2

我認爲這是一個錯字。使用這個:

<% @recipes.each do |**recipe**| %> 
    <tr> 
    <td><%= recipe.vegetable.name %><td> 
    <td><%= recipe.protein.name %><td> 
    </tr> 
<%end%>