简体   繁体   English

Ruby on Rails 中连接表的正确关联是什么?

[英]What is the correct associations for join tables in Ruby on Rails?

So I have a recipes app that I'm working on and I can't understand why I'm not getting something to display.所以我有一个正在开发的食谱应用程序,但我不明白为什么我没有显示一些东西。 I'm using Recipe as a join table and for simplicity I only included it along with three other tables.我使用 Recipe 作为连接表,为简单起见,我只将它与其他三个表一起包含在内。 For purposes of the app the only thing I'm wanting to display is the name associated on each model.就应用程序而言,我唯一想显示的是每个模型上关联的名称。

I have the following models: Vegetable Protein Starch Recipe我有以下型号: 植物蛋白淀粉配方

Recipe is acting as the join table and has: Recipe 充当连接表并具有:

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

Each of the other model has:其他每个模型都有:

has_one :recipe

So my thought for the view was a simple iteration所以我对视图的想法是一个简单的迭代

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

Yet nothing is coming up beyond the headers.然而,除了标题之外什么也没有出现。 I've done various things with the iteration like changing recipes to recipe, passing recipe as an argument at the end of each column.我在迭代中做了很多事情,比如将配方更改为配方,在每列的末尾将配方作为参数传递。 I've made sure that my Recipe controller has the following:我已确保我的 Recipe 控制器具有以下内容:

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

Am I wrong in my associations and that's why it's not presenting anything?我的关联是否错了,这就是为什么它没有呈现任何东西?

I think it is a typo.我认为这是一个错字。 Use this:用这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM