Right now, I have a do loop for a task model in my view:
<% @tasks.each do |t| %>
<div class="task purple">
<%= link_to t.name, edit_task_path(t) %>
</div>
<% end %>
I need some help making it so the div class changes based off my model data. I’ve tried doing this:
<% @tasks.each do |t| %>
<div class="task " + t.theme>
<%= link_to t.name, edit_task_path(t) %>
</div>
<% end %>
But that failed miserably. I then read up on content tag, and was trying something like:
content_tag(:div, :class => t.theme){<%= link_to t.name, edit_task_path(t) %>}
in the loop but that was rendering it as text.
Anyway, just need some help learning how to change html tag attributes based off the model data? Is this something I would build a view helper for?
Thanks
The reason your first approach isn’t working, is that you haven’t included the ERb tags (
<%= %>) to output the return value fromt.theme. In order for that to show up in the output, you need to do this:Remember: ERb knows nothing about HTML, it just scans the entire file for
<% %>or<%= %>tags, and evaluates the ruby code within them.content tagshould work the way you’ve got it there, but I’d rather pass in the tag’s content as the third parameter instead of using a block (I assume you’re using Rails 3, the syntax is slightly different in previous versions):