为什么 Rails 会显示页面上的所有信息? [英] Why is Rails displaying all the information on the page?

查看:37
本文介绍了为什么 Rails 会显示页面上的所有信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rails 3.2 应用程序中有以下代码:

I have the following code in my rails 3.2 app:

 <ul>            
          <% user_to_show.topics.each do |topic| %>
          <li> <%= link_to topic.name, topic %> 
              <ul>
                <%= topic.nodes.each do |node| %>
                  <li> <%= link_to node.title, node %> </li>
                <% end %>
              </ul>
          </li>
          <% end %>    
  </ul>

当我转到用户页面时,我希望它只显示指向每个主题和该主题中节点的链接.它会这样做,但它也会在最后显示每个节点的实际数据(见下文).怎么来的,我该如何解决?

When I go to a user page, I would expect it to just display a link to each topic and the nodes in that topic. It does that, but then it also displays the actual data of each node at the end (see below). How come and how do I fix it?

Sample Topic  
node1  
node2 

[#<Node id: 1, title: "node1", intro_content: "sample content", user_id: 2, date_modified: nil, created_at: "2012-07-19.." [etc..] #<Node id: 2, title: "node2", intro_content: "sample 2 content..", user_id: 2, etc.. ]

推荐答案

因为这一行:

<%= topic.nodes.each do |node| %>

每个 Ruby 语句都是一个表达式.所以每个方法都会返回一些东西(即使它是零).因此,如果您调用每个",它将在循环完成后返回数组.所以,当你使用 <%= 时,它会在循环结束后写入这个数组.

Every Ruby statement is an expression. So every method returns something (even if it's nil). So if you call an "each", it will return the Array after the loop is done. So, as you used <%=, it will write this array after the loop is over.

您应该使用过:

<% topic.nodes.each do |node| %>

这篇关于为什么 Rails 会显示页面上的所有信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆