Search code examples
htmlruby-on-railsredundancy

How to improve redundancy in HTML and ROR with partials?


I have these 2 partials written for my Ruby on Rails application. Is there any clever way two merge them into one? Copy pasting one partial into the other is not allowed

What I would like to have is something like this:

@member = 'Player'
render 'team/team_partial'

@member = 'Staff'
render 'team/team_partial'

Or any other clever way that deals with the redundancy of the code is accepted.

  1. .row  
      .col-md-6.info_block    
            .row  
              .col-md-10.col-md-offset-1  
                = icon 'user'  
                .caption  
                  h3.col-md-6.col-md-offset-3   
                    |Players:  
                  table  
                    tbody  
                      - @team.players.each do |player|  
                        tr  
                          td = icon 'check'  
                          td   
                            |Name:   
                            a href= '#'  
                              = player.name  
                          td   
                            = form_for(@team, url: team_path(@team), method: :delete, html:{class:"team_member_form"}) do |f|  
                              = hidden_field_tag :player, player.id  
                              = f.submit "Delete", class:"btn btn-danger btn-sm", data: { confirm: "Are you sure?" }  
                  button type="button" class="btn btn-success showModal" data-toggle="modal" data-target="#exampleModal" data-member="Player"  
                    |Add Player  

  2. 2.

.row
          .col-md-10.col-md-offset-1
            = icon 'user'
            .caption
              h3.col-md-6.col-md-offset-3 
                |Staff:
              table
                tbody
                  - @team.staff.each do |staff|
                    tr
                      td = icon 'check'
                      td 
                        |Name: 
                        a href= #
                          = staff.name
                      td 
                        = form_for(@team, url: team_path(@team), method: :delete, html:{class:"team_member_form"}) do |f|
                          = hidden_field_tag :staff, staff.id
                          = f.submit "Delete", class:"btn btn-danger btn-sm", data: { confirm: "Are you sure?" }
              button type="button" class="btn btn-success showModal" data-toggle="modal" data-target="#exampleModal" data-member="Staff"
                i class="glyphicon glyphicon-plus"
                |Add Staff


Solution

  • Call the partial this way, member_type define the type of member "staff" or "players"

    <%= render "team/team_partial", :locals => {:team => @member, :member_type => @member.type} %>
    

    In view

    .row
          .col-md-10.col-md-offset-1
            = icon 'user'
            .caption
              h3.col-md-6.col-md-offset-3 
                |#{member_type}:
              table
                tbody
                  - @team.#{member_type}.each do |member|
                    tr
                      td = icon 'check'
                      td 
                        |Name: 
                        a href= #
                          = member.name
                      td 
                        = form_for(@team, url: team_path(@team), method: :delete, html:{class:"team_member_form"}) do |f|
                          = hidden_field_tag #{member_type}.to_s, member.id
                          = f.submit "Delete", class:"btn btn-danger btn-sm", data: { confirm: "Are you sure?" }
              button type="button" class="btn btn-success showModal" data-toggle="modal" data-target="#exampleModal" data-member= #{member_type}
                i class="glyphicon glyphicon-plus"
                |Add #{member_type}