为什么.rails视图中的.each完成后为什么要重复数组? [英] Why is .each repeating the array after completion in my rails view?

查看:88
本文介绍了为什么.rails视图中的.each完成后为什么要重复数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的rails视图页面中,我有以下循环,该循环应遍历我的tag_list数组并打印每个标签:

In my rails view page, I have the following loop that should loop through my tag_list array and print each tag:

<%= @user.profile.tag_list.each do |tag| %>
    <%= tag %>
<% end %>

由于某种原因,它会在打印每个单独的标签后重复该数组.例如,此数组具有两个元素:

For some reason, it repeats the array after it prints each individual tag. For example, this array has two elements:

["ruby", "python"]

每个方法的输出是"ruby​​python ruby​​,python".输出应该只是"ruby​​ python".我该如何解决?

The output of the each method is "rubypython ruby,python". The output should just be "ruby python". How do I fix this?

顺便说一句,我使用的是行为可标记的gem来生成标签,但这应该没有什么区别,因为它只是一个简单的数组.

By the way, I am using the acts-as-taggable-on gem to generate the tags, but that should not make a difference since it is just a simple array.

推荐答案

您应删除等号

<%= @user.profile.tag_list.each do |tag| %>

<% @user.profile.tag_list.each do |tag| %>

嵌入的红宝石在运行之后正在打印您的每个块,因此您将获得运行.eachtag

the embedded ruby is printing your each block after it's run, so you're getting results of .each being run as well as the tag

这篇关于为什么.rails视图中的.each完成后为什么要重复数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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