[英]ruby on rails - Do Loop in a controller
I would like to display every files that belong to one given project (the relationship works fine, could check it using the Rails console). 我想显示属于一个给定项目的每个文件(这种关系很好,可以使用Rails控制台进行检查)。 Here is my 'Project' controller , I may need a do loop (to loop through each files , for 1 project) but I'm not sure : 这是我的“项目”控制器,我可能需要执行do循环(以循环方式遍历1个项目的每个文件),但我不确定:
def show
@project = Project.find(params[:id])
@pfile = Project.find(params[:id]).Pfiles.find(:all)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @project }
format.xml { render :xml => @pfile }
end
end
This is my 'Project' view : 这是我的“项目”视图:
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @project.name %>
</p>
<p>
<b>Description:</b>
<%= @project.description %>
</p>
<p>
<b>Files:</b>
<%= @project.pfile.name %>
</p>
<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>
Thanks :) 谢谢 :)
If the relationship is set up correctly you can do this: 如果关系建立正确,则可以执行以下操作:
@pfiles = @project.pfiles
<p>
<b>Files:</b>
<% @pfiles.each do |pfile| %>
<%= pfile.name %>
<% end %>
</p>
easier yet... rely on the magic of rails. 更容易...依靠Rails的魔力。
in the controller 在控制器中
@project = Project.find(params[:id])
@pfiles = @project.pfiles
then in the view 然后在视图中
<b>Files:</b>
<%= render @pfiles %>
then in views/pfiles/_pfile.html.erb 然后在views / pfiles / _pfile.html.erb中
<%= pfile.name %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.