Mau Ruiz
Mau Ruiz

Reputation: 777

Rails 3 tag_list.each

I have a problem trying to make a list from a acts_as_taggable_on tag_list I have tag list array, and I want to list it so im trying this:

<%= proyects.tag_list.each do |tagsx| %>
* <%= tagsx %>  <br>
<% end %>

And I get the list im looking for, but also the whole array again... When it renders, looks like this..

* AJAX
* Rails
* Heroku
* Prototype
AJAX, Rails, Heroku, Prototype

Any ideas on getting rid of the last line? Or do you guys know a more efficient way of achieving this?

Thanks in advance.

Upvotes: 2

Views: 269

Answers (2)

mu is too short
mu is too short

Reputation: 434665

Change this:

<%= proyects.tag_list.each do |tagsx| %>

to this:

<% proyects.tag_list.each do |tagsx| %>

You don't want to output the return value of the .each call, just the elements of the array. Calling Array#each with a block returns the array (as you are):

each {|item| block } → ary
each → an_enumerator
Calls block once for each element in self, passing that element as a parameter. If no block is given, an enumerator is returned instead.

and that's were the comma delimited list is coming from.

Upvotes: 4

Tilo
Tilo

Reputation: 33732

because you have a typo in your code :-)

<%- proyects.tag_list.each do |tagsx| %>
* <%= tagsx %>  <br>
<% end %>

see the difference?

no '=' after the first % sign

%= means that the result of a Ruby expression is returned to the view

%- means that the Ruby expression is evaluated, but no result is returned

The code in your question gets "proyects.tag_list" , executes the loop, during which it prints out the individual tags, and then returns the whole array to the view because of the '='

Upvotes: 1

Related Questions