Rails 部分视图重复显示
Rails partial view repeat display
这是我的一部分 index.html
<div class="col-md-8">
<% @books.each do |book| %>
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
 
<% end %>
</div>
</div>
<% end %>
</div>
基本上,它会显示3本书,而且效果很好。
然后,我将该代码移动到 _book.html.erb
并将上面的代码编辑到
<%= render @books %>
但是重复3次,即显示9本书。顺序是 [1st,2nd,3rd] [1st,2nd,3rd] [1st,2nd,3rd] 就像这张图。
更新
更新index.html.erb
<div class="clearfix">
<div class="col-md-12">
<%= render @books %>
<div class="col-md-4">
<h3>Genre (Click to filter books)</h3>
<br>
<li>
<span class = 'label label-danger'>
<%= link_to "No filter" ,books_path, style: 'color:#FFFFFD' %>
</span>
<br>
<br>
</li>
<% @genres.each do |genres| %>
<li>
<span class = 'label label-primary' style="color:#FFFFFD">
<%= link_to genres.name ,books_path(filter: genres.name),style: 'color:#FFFFFD' %>
</span>
</li>
<br>
<% end %>
</div>
</div>
<!-- clearfix -->
</div>
尝试以下操作:
<div class="col-md-8">
<%= render @books %>
</div>
并确保 <% @books.each do |book| %>
及其 <% end %>
标签不在部分中。
编辑
#index.html.erb
<div class="col-md-8">
<%= render @books %>
</div>
# _book.html.erb
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
 
<% end %>
</div>
</div>
这是我的一部分 index.html
<div class="col-md-8">
<% @books.each do |book| %>
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
 
<% end %>
</div>
</div>
<% end %>
</div>
基本上,它会显示3本书,而且效果很好。
然后,我将该代码移动到 _book.html.erb
并将上面的代码编辑到
<%= render @books %>
但是重复3次,即显示9本书。顺序是 [1st,2nd,3rd] [1st,2nd,3rd] [1st,2nd,3rd] 就像这张图。
更新
更新index.html.erb
<div class="clearfix">
<div class="col-md-12">
<%= render @books %>
<div class="col-md-4">
<h3>Genre (Click to filter books)</h3>
<br>
<li>
<span class = 'label label-danger'>
<%= link_to "No filter" ,books_path, style: 'color:#FFFFFD' %>
</span>
<br>
<br>
</li>
<% @genres.each do |genres| %>
<li>
<span class = 'label label-primary' style="color:#FFFFFD">
<%= link_to genres.name ,books_path(filter: genres.name),style: 'color:#FFFFFD' %>
</span>
</li>
<br>
<% end %>
</div>
</div>
<!-- clearfix -->
</div>
尝试以下操作:
<div class="col-md-8">
<%= render @books %>
</div>
并确保 <% @books.each do |book| %>
及其 <% end %>
标签不在部分中。
编辑
#index.html.erb
<div class="col-md-8">
<%= render @books %>
</div>
# _book.html.erb
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
 
<% end %>
</div>
</div>