Why is .each repeating the array after completion in my rails view? [duplicate]

一笑奈何 提交于 2019-12-24 02:23:43

问题


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"]

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

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.


回答1:


you should remove the equals sign

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

to

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

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



来源:https://stackoverflow.com/questions/19911474/why-is-each-repeating-the-array-after-completion-in-my-rails-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!