如何停止在 #show 中复制部分内容?
How to stop count from duplicating partials in #show?
出于某种原因,我得到了 show.html.erb
的副本。重复项恰好与 .count
一致,即使我从显示文件中删除了 .count
。
users/show.html.erb
<% 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
如图所示,如果我养成3个习惯,就会出现3个习惯表。如果我创建 4、5,那么同样的事情会发生。其余部分也是如此,例如值(@valuations)正在创建 12 次重复。
habits/_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>
你有什么想法吗?我很困惑。
你可以试试这个:
<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 %>
它们是同一个控制器的一部分吗?如果是这样,调用不带变量的部分...
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render 'habits' %>
<% end %>
如果不是,用局部变量调用部分:
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render 'habits', :locals => {:habits => @habits} %>
<% end %>
如果是这种情况,您必须更改部分以停止对@habits 使用基于@的变量。
这是渲染对象集合的正常行为。呈现集合中每个对象的部分内容。
查看 guides.rubyonrails.org 关于布局和渲染的第 3.4.5 节...
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
可能应该编辑习惯部分以仅显示集合中每个元素要显示的内容。如果它当前只显示第一个元素的信息,那么您可能没有正确地将对象传递给部分。请参阅上述文档中的第 3.4.6 节。
出于某种原因,我得到了 show.html.erb
的副本。重复项恰好与 .count
一致,即使我从显示文件中删除了 .count
。
users/show.html.erb
<% 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
如图所示,如果我养成3个习惯,就会出现3个习惯表。如果我创建 4、5,那么同样的事情会发生。其余部分也是如此,例如值(@valuations)正在创建 12 次重复。
habits/_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>
你有什么想法吗?我很困惑。
你可以试试这个:
<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 %>
它们是同一个控制器的一部分吗?如果是这样,调用不带变量的部分...
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render 'habits' %>
<% end %>
如果不是,用局部变量调用部分:
<% if @user.habits.any? %>
<h2>Habit Challenges (<%= @user.habits.count %>)</h2>
<%= render 'habits', :locals => {:habits => @habits} %>
<% end %>
如果是这种情况,您必须更改部分以停止对@habits 使用基于@的变量。
这是渲染对象集合的正常行为。呈现集合中每个对象的部分内容。
查看 guides.rubyonrails.org 关于布局和渲染的第 3.4.5 节...
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
可能应该编辑习惯部分以仅显示集合中每个元素要显示的内容。如果它当前只显示第一个元素的信息,那么您可能没有正确地将对象传递给部分。请参阅上述文档中的第 3.4.6 节。