0

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
Justin
  • 31
  • 5

1 Answers1

0

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}
Sonalkumar sute
  • 2,565
  • 2
  • 17
  • 27
  • Thank you! I really am grateful! – Justin Feb 20 '15 at 11:23
  • your're welcome, just see in view somewhere you need `player` rather than `players` you can always use `'players'.singularize` – Sonalkumar sute Feb 20 '15 at 11:26
  • Hey Sontya! I tried running this line of code but it never works: @team.#{member_type}.each – Justin Feb 20 '15 at 13:59
  • It does not do anything. Do you have any idea on how I may convert a method name from a string> For example if I have the method "members" to convert it so that I may cal team.members? – Justin Feb 20 '15 at 14:00
  • I have managed to found the answer! the "send" method achieves exactly what i want – Justin Feb 20 '15 at 14:05
  • `@team.instance_variable_set('@'+#{member_type}, new_value)`, try something like this. Thats good about to suggest you that only. Still it not work for you, let me know – Sonalkumar sute Feb 20 '15 at 14:07
  • 1
    I finally managed to rewrite my code using your help! Thank you! – Justin Feb 20 '15 at 15:07