0

I'm just learning how to move view code into decorators using the draper gem. I'm having trouble figuring out how to move .each into the decorator.

<% @project.users.each do |p| %>
    <small class="btn btn-mini">
        <%= link_to "#{p.first_name} #{p.last_name}", "mailto:#{p.email}" %>
    </small>
    <br>
<% end %>

I tried the following:

View code:

<%= @project.assigned_users %>

Decorator code:

def assigned_users
    model.users.each do |p|
        h.content_tag :small, class: 'btn btn-mini' do
            h.link_to "#{p.first_name} #{p.last_name}", "mailto:#{p.email}"
        end
    end
end

The result is the entire user hash instead of the first_name last_name button I'm looking for. Maybe .each doesn't belong in the decorator? Any help would be appreciated.

prodigerati
  • 597
  • 8
  • 26

1 Answers1

0

each returns the original hash. You need instead to caoncatenate the result of your block:

def assigned_users
    model.users.map do |p|
        h.content_tag :small, class: 'btn btn-mini' do
            h.link_to "#{p.first_name} #{p.last_name}", "mailto:#{p.email}"
        end
    end.flatten.join.html_safe
end
prodigerati
  • 597
  • 8
  • 26
rewritten
  • 16,280
  • 2
  • 47
  • 50
  • That did the trick, thank you. NOTE: I had to add `.html_safe` on the end line (end.flatten.join.html_safe) so it would display correctly. – prodigerati Nov 17 '13 at 22:32