加倍加额外输出,为什么? [英] Doubled plus extra output, why?

查看:36
本文介绍了加倍加额外输出,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下代码时,我的页面上的输出翻了一番,我没有看到什么?我不想要 [] 里面的文字.

I am getting doubled output on my page when the following code executes, what am I not seeing? I do not want the text inside the [].

<dt>Publications</dt>
            <dd>
                <ul>
                    <%= @person.pubs.each do |pub| %>
                        <li><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>, <%= pub.link %></li>
                    <% end %>
                </ul>
            </dd>

输出如下所示:

出版物

    Thiebaud N, Johnson MC, Butler JL, Bell GA, Ferguson KL, Fadool AR, Fadool JC, Gale AM, Gale DS, Fadool DA, Hyperlipidemic diet causes loss of olfactory sensory neurons, reduces olfactory discrimination, and disrupts odor-reversal learning., J. Neurosci., 2014,
    [#<Pub id: 1000, person_id: 7, pubmed_id: nil, journal: "J. Neurosci.", title: "Hyperlipidemic diet causes loss of olfactory sensor...", auth_id: "Fadool DA", authors: "Thiebaud N, Johnson MC, Butler JL, Bell GA, Ferguso...", pages: "34(20):6970-84", date: "2014", type: nil, link: nil, notes: nil, created_at: nil, updated_at: nil>] 

此文本还包括我的代码中未调用的信息.

This text also includes information that is not being called in my code.

推荐答案

<%= @person.pubs.each do |pub| %>
    <li><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>, <%= pub.link %></li>
<% end %>

应该

<% @person.pubs.each do |pub| %>
    <li><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>, <%= pub.link %></li>
<% end %>

(请注意,在执行 .each 循环的行上没有 = 符号).

(notice there is no = sign on the line where you perform the .each loop).

<%= 是一个打印语句..each 返回集合,因此您的情况下的结果是双重输出:一个来自内部循环,另一个来自 .each.

<%= is a print statement. .each returns the collection, therefore the result in your case is a double output: one from the inner loop and one from .each.

这篇关于加倍加额外输出,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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