出于某种原因,我得到show.html.erb
的副本。即使我从show文件中删除.count
,重复项也恰好与.count
一致。
用户/ show.html.er B'/ P>
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render @habits %>
<% end %>
<% if @user.valuations.any? %>
<h2>Values (<%= @user.valuations.count %>)</h2>
<%= render @valuations %>
<% end %>
<% if @user.goals.any? %>
<h2>Current Goals (<%= @user.goals.unaccomplished.count %>)</h2>
<%= render @unaccomplished_goals %>
<% end %>
<% if @user.goals.any? %>
<h2>Accomplished Goals (<%= @user.goals.accomplished.count %>)</h2>
<%= render @accomplished_goals %>
<% end %>
<% if @user.quantifieds.any? %>
<h2>Stats: Monthly Average (<%= @user.quantifieds.count %>)</h2>
<%= render @averaged_quantifieds %>
<% end %>
<% if @user.quantifieds.any? %>
<h2>Stats: Instances (<%= @user.quantifieds.count %>)</h2>
<%= render @instance_quantifieds %>
<% end %>
users_controller.rb
def show
@user = User.find(params[:id])
@habits = @user.habits.all
@valuations = @user.valuations.all
@accomplished_goals = @user.goals.accomplished.all
@unaccomplished_goals = @user.goals.unaccomplished.all
@averaged_quantifieds = @user.quantifieds.averaged.all
@instance_quantifieds = @user.quantifieds.instance.all
end
正如你在图片中看到的那样,如果我养成三种习惯,就会显示出三种习惯。如果我创建4,5,那么就会发生同样的事情。其余部分也是如此,例如值(@valuations)正在创建12次重复。
习惯/ _habit.html.erb
<!-- Default bootstrap panel contents -->
<div id="valuations" class="panel panel-default">
<div class="panel-heading"><h4><b>HABITS</b></h4></div>
<!-- Table -->
<table>
<thead>
<tr>
<th>Level</th>
<th>Left</th>
<th>Strike</th>
<th>Trigger</th>
<th>Action</th>
<th>Target</th>
<th>Reward</th>
<th>Days</th>
</tr>
</thead>
<tbody>
<% @habits.each do |challenged| %>
<tr>
<td><%= challenged.current_level %></td>
<td>
<%= link_to edit_habit_path(challenged) do %>
<%= [params[:missed]].flatten.length %>
<% end %></td>
<td><%= challenged.trigger %></td>
<td class="category">
<b><%= raw challenged.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %></b>
</td>
<td><%= challenged.target %></td>
<td class= "committed">
<%= challenged.committed.map { |d| d.titleize[0,3] }.join ', ' %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
&#13;
你有什么想法吗?我感到困惑。
答案 0 :(得分:0)
你可以试试这个:
<h2>Habit Challenges (<%= @habits.count %>)</h2>
<%= render @habits %>
<h2>Values (<%= @valuations.count %>)</h2>
<%= render @valuations %>
<h2>Current Goals (<%= @unaccomplished_goals.count %>)</h2>
<%= render @unaccomplished_goals %>
<h2>Accomplished Goals (<%= @accomplished_goals.count %>)</h2>
<%= render @accomplished_goals %>
<h2>Stats: Monthly Average (<%= @averaged_quantifieds.count %>)</h2>
<%= render @averaged_quantifieds %>
<h2>Stats: Instances (<%= @instance_quantifieds.count %>)</h2>
<%= render @instance_quantifieds %>
更改您的节目动作:
def show
@user = User.find(params[:id])
@habits = @user.habits
@valuations = @user.valuations
@accomplished_goals = @user.goals.accomplished
@unaccomplished_goals = @user.goals.unaccomplished
@averaged_quantifieds = @user.quantifieds.averaged
@instance_quantifieds = @user.quantifieds.instance
end
更改您的部分,再次停止迭代@habits
:
<tbody>
<tr>
<td><%= habit.current_level %></td>
<td><%= link_to edit_habit_path(habit) do %>
<%= [params[:missed]].flatten.length %><% end %>
</td>
<td><%= habit.trigger %></td>
<td class="category"><b><%= raw habit.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %></b></td>
<td><%= habit.target %></td>
<td class= "committed"><%= habit.committed.map { |d| d.titleize[0,3] }.join ', ' %></td>
</tr>
</tbody>
<强>更新强>:
另一种方式,只会重复tbody
(根据您的要求)。
<h2>Habit Challenges (<%= @habits.count %>)</h2>
<%= render partial: 'habit', locals: {habits: @habits} %>
然后改变你的习惯:
<% habits.each do |habit| %>
<tbody>
<tr>
...
...
</tr>
</tbody>
<% end %>
答案 1 :(得分:0)
它们是同一控制器的一部分吗?如果是这样,请在没有变量的情况下调用partial ...
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render 'habits' %>
<% end %>
如果没有,请使用局部变量调用partial:
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render 'habits', :locals => {:habits => @habits} %>
<% end %>
如果是这种情况,你必须改变部分停止使用@habits的基于@的变量。
答案 2 :(得分:0)
如果渲染对象集合,那就是正常行为。将呈现集合中每个对象的部分内容。
请参阅guides.rubyonrails.org关于布局和渲染的第3.4.5节......
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
可能会编辑习惯部分,以便仅显示您要在集合中的每个元素显示的内容。如果它当前仅显示第一个元素的信息,那么您可能无法正确地将对象传递给partial。见上文所述的3.4.6节。