Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top