遍历实例变量打印不需要的东西 [英] Iterating over Instance variable printing unwanted things

查看:91
本文介绍了遍历实例变量打印不需要的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题详细信息在这里

My questions details are here

针对@ cart.items.each中的nil:NilClass的未定义方法项"

我添加的其他内容是应用程序控制器中的购物车方法,并且还使其成为 helper_method ,以使其在整个应用程序的所有视图中均可用

The extra things I added were a cart method in application controller and also made it helper_method to make it available in all views across the application

helper_method :cart

def cart
    @cart = Cart.find(session[:cart_id])
  end

在我看来,我像这样遍历@cart变量

When in my view I iterate over the @cart variable like this

    <%= cart.items.each do |item| %>
    <tr>
        <td><%= item.product.title%></td>
        <td><%= item.product.price%></td>
    </tr>
    <% end %>

它显示了

为什么要打印上面的东西?我知道这是 item.product.title 和所有此类变量的输出.如何删除它?

Why is printing that upper thing? I know that is the output of item.product.title and all such variables. How to remove that?

推荐答案

为什么要打印上面的东西?

Why is printing that upper thing?

因为您正在告诉它.当用<%= %>包围红宝石代码时,就是在告诉它呈现随附代码的结果.由于您将其放在each块周围,因此会显示each的结果,这是它被调用的集合.

Because you are telling it to. When you surround ruby code with <%= %> you are telling it to render the result of the enclosed code. Since you are putting it around the each block it is displaying the result of each, which is the collection it was called on.

要删除它,只需使用<% %>即可.

To remove it, just use <% %> instead.

这篇关于遍历实例变量打印不需要的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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