简体   繁体   English

当一个模型属于 Rails 中的另一个模型时,如何列出它?

[英]How can I list a model when it belongs to a another model in rails?

I have two models: Day and Lecture, lecture belongs to day and day has many lectures, I want list all the lecture in each day:我有两个模型:Day 和 Lecture,talk 属于 day,day 有很多讲座,我想列出每天的所有讲座:

<% @days.each do |day| %>
<tr>
  <td><%= day.name %></td>
  <td><%= day.date %></td>
  <td><%= link_to 'Destroy', day_path(day),
                method: :delete,
                data: { confirm: 'This will delete all the lectures in this day. Do you want to continue?' } %></td>


  <br/>
  <%= render @day.lectures %>
  <br/>
</tr>

but it does not work with '@day.lectures'.但它不适用于“@day.lectures”。

btw, i already have _lecture.html.erb file and it should be a problem of how to write it.顺便说一句,我已经有了_lecture.html.erb 文件,应该是如何编写它的问题。 If not, I can put on my routes or more information.如果没有,我可以提供我的路线或更多信息。 Thanks!谢谢!

Remove @ from your @day instance so that the line is从您的@day实例中删除@ ,以便该行是

<%= render day.lectures %>

You're inside the iteration of @days and each day is referenced as day你在@days的迭代中,每一天都被称为day

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

相关问题 在 Rails 中,如何验证属于另一个 model 的 model 上的属性? - In Rails, how do I validate a property on a model that belongs to another model? rails-无法访问属于另一个模型的模型 - rails - Can't access a model that belongs_to another model 我可以有一个 model 属于另一个 model 在导轨中的两个不同名称下吗? - Can i have a model that belongs_to another model under two different names in rails? 在Rails中,如何将一个模型两次连接到一个多态属于的另一个模型? - In Rails, how to connect a model twice to another model that it polymorphically belongs_to? 我如何获得一个模型的关系,该模型属于rails中的另一个模型 - How do I get a relation for a model that belongs_to another model in rails Rails模型属于一个模型或另一个模型 - Rails model belongs to either one model or another 如何挂钩销毁属于另一个模型的模型? - how to hook for destroy of a model that belongs to another model? Rails和RABL通过它所属的另一个模型对子级进行排序 - Rails and RABL sort child by another model that it belongs to rails-继承类属于另一个模型 - rails - Inheritanting class belongs to another model Rails模型具有并且属于许多列表类型 - Rails model has and belongs to many list types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM