如何改善 HTML 中的冗余和部分 ROR?
How to improve redundancy in HTML and ROR with partials?
我在 Rails 申请中为我的 Ruby 编写了这两个部分。有什么巧妙的方法可以将它们合二为一吗?不允许将一个部分复制粘贴到另一个
我想要的是这样的:
@member = 'Player'
渲染 'team/team_partial'
@member = 'Staff'
渲染 'team/team_partial'
或任何其他处理代码冗余的聪明方法都被接受。
-
.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.
.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
这样调用partial,member_type定义成员类型"staff"或"players"
<%= render "team/team_partial", :locals => {:team => @member, :member_type => @member.type} %>
可见
.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}
我在 Rails 申请中为我的 Ruby 编写了这两个部分。有什么巧妙的方法可以将它们合二为一吗?不允许将一个部分复制粘贴到另一个
我想要的是这样的:
@member = 'Player'
渲染 'team/team_partial'
@member = 'Staff'
渲染 'team/team_partial'
或任何其他处理代码冗余的聪明方法都被接受。
.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.
.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
这样调用partial,member_type定义成员类型"staff"或"players"
<%= render "team/team_partial", :locals => {:team => @member, :member_type => @member.type} %>
可见
.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}