问题
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