title: How do I prevent very similar if/else Ruby code?
body: <p>I have a Rails 5 app where I have many if/else statements that almost outputs the same content. Here is an example:</p>
  
  <pre><code>- if @url.include?('coaches')
    .page-container
      .container.margin-bottom-xs.spacer-minus
        .row.spacer-md
          .col-md-12
            .margin-bottom-sm
              .row
                - @am_coaches.each do |user|
                  .col-md-4
                    .small-card.shadow.margin-bottom-sm
                      = image_tag(user.avatar.url, class: 'medium-img')
                      = link_to [user.team, user] do
                        .small-overlay
                          .row
                            .col-md-11
                              .post-category
                                - if user.city?
                                  = user.city
                                  |,&amp;nbsp;
                                - if user.country?
                                  = user.country
                              .post-title.text-default
                                = user.name
                              .post-time-ago.text-muted
                                - unless user.age_group.blank?
                                  = user.age_group
                                  |,&amp;nbsp;
                                = user.team.name
  
                            .col-md-1.text-right
                              - if user.facebook_url
                                .post-category
                                  = link_to "http://www.facebook.com/#{user.facebook_url}",
                                  target: '_blank'
                                    i.icon-facebook
                              - if user.twitter_url
                                .post-category
                                  = link_to "http://www.twitter.com/#{user.twitter_url}",
                                  target: '_blank'
                                    i.icon-twitter
                              - if user.instagram_handle
                                .post-category
                                  = link_to "http://www.instagram.com/#{user.instagram_handle}",
                                  target: '_blank'
                                    i.icon-instagram
  
  - elsif @url.include?('users')
    .spacer-lg
      - @skills.uniq.sort.reverse.each do |skill|
        .container.spacer-sm
          span.section-heading.text-semibold
            = skill.humanize
  
          .margin-bottom-sm
            .row.spacer-md
              - @am_users[skill].each do |user|
                .col-md-4
                  .small-card.shadow.margin-bottom-sm
                    = image_tag(user.avatar.url, class: 'medium-img')
                    = link_to [user.team, user] do
                      .small-overlay
                        .row
                          .col-md-11
                            .post-category
                              - if user.city?
                                = user.city
                                |,&amp;nbsp;
                              - if user.country?
                                = user.country
                            .post-title.text-default
                              = user.name
                            .post-time-ago.text-muted
                              - unless user.age_group.blank?
                                = user.age_group
                                |,&amp;nbsp;
                              = user.team.name
  
                          .col-md-1.text-right
                            - if user.facebook_url
                              .post-category
                                = link_to "http://www.facebook.com/#{user.facebook_url}",
                                target: '_blank'
                                  i.icon-facebook
                            - if user.twitter_url
                              .post-category
                                = link_to "http://www.twitter.com/#{user.twitter_url}",
                                target: '_blank'
                                  i.icon-twitter
                            - if user.instagram_handle
                              .post-category
                                = link_to "http://www.instagram.com/#{user.instagram_handle}",
                                target: '_blank'
                                  i.icon-instagram
  </code></pre>
  
  <p>As you can see, 80% of this is duplicated code. I'm trying to improve the way I write my apps and would love to know how I can make this nicer. </p>
  
  <p>My initial thought was to take the loops and put them into a shared view. However, if doing so the variables are not recognised. </p>
  
  <p>Any suggestions on how I should approach this now and in the future?</p>