How to show app data in erb template?
-
19-09-2019 - |
Question
A first look I thought erb accepts any Ruby code, but I've got this strange behaviour...
I have an array [of tags for my article], and I want to make a nice display for them. So I'm writing something like this:
<ul>
<% @post.tags.each do |item| %>
<li>item</li>
<% end %>
</ul>
The wrong output looks like this:
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
...
</ul>
Where I am wrong? Any suggestions how to make a proper iteration?
Solution
You forgot the <%= %>
to display the value of item
:
<ul>
<% @post.tags.each do |item| %>
<li><%= item %></li>
<% end %>
</ul>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow