简体   繁体   English

如何用另一个模型的ID渲染所有实例?

[英]How to render all instances with an id from another model?

I have a model :folder that belongs to :user and takes a user_id. 我有一个属于:user的模型:folder并带有一个user_id。 A user can create as many folders as they want, and the homepage renders all folders that have the user_id of the current user. 用户可以创建任意数量的文件夹,并且主页呈现具有当前用户的user_id的所有文件夹。 However, using this code on the homepage: 但是,在首页上使用以下代码:

<ol class="folders-vertical">
  <%= render @folders %>
</ol>

with this instance variable in the homepage controller: 在主页控制器中使用以下实例变量:

@folders = current_user.folders

It also renders the :folder model itself. 它还会渲染:folder模型本身。 So, each user's first folder is the :folder model instead of the actual first folder they create. 因此,每个用户的第一个文件夹是:folder模型,而不是他们创建的实际第一个文件夹。 How can I avoid rendering this? 我如何避免呈现此内容?

您可以使用如下形式:

@folders = Folder.where(user_id: current_user.id).drop(1)

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

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