如何在侧边栏中停止双重渲染?
代码是双重渲染,如下所示:
什么时候应该只列出一次:
Ran 1 miles Apr Journal 1 days Apr
视图/布局/ _stats.html.erb
<div class=""> <span class="">
代码是双重渲染,如下所示:
什么时候应该只列出一次:
Ran 1 miles Apr Journal 1 days Apr
视图/布局/ _stats.html.erb
<div class=""> <span class="">
application_controller
def set_stats @averaged_quantifieds = current_user.quantifieds.averaged if current_user @instance_quantifieds = current_user.quantifieds.instance if current_user end
结果是Quantifieds的nested_attribute。
quantifieds_controller
class QuantifiedsController < ApplicationController before_action :set_quantified, only: [:show, :edit, :update, :destroy] before_action :logged_in_user, only: [:create, :destroy] def index if params[:tag] @quantifieds = Quantified.tagged_with(params[:tag]) else @quantifieds = Quantified.joins(:results).all @averaged_quantifieds = current_user.quantifieds.averaged @instance_quantifieds = current_user.quantifieds.instance end end def show end def new @quantified = current_user.quantifieds.build end def edit end def create @quantified = current_user.quantifieds.build(quantified_params) if @quantified.save redirect_to quantifieds_url, notice: 'Quantified was successfully created' else @feed_items = [] render 'pages/home' end end def update if @quantified.update(quantified_params) redirect_to quantifieds_url, notice: 'Goal was successfully updated' else render action: 'edit' end end def destroy @quantified.destroy redirect_to quantifieds_url end private def set_quantified @quantified = Quantified.find(params[:id]) end def correct_user @quantified = current_user.quantifieds.find_by(id: params[:id]) redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if @quantified.nil? end def quantified_params params.require(:quantified).permit(:categories, :metric, :date, :comment, :private_submit, :tag_list, results_attributes: [:id, :result_value, :date_value, :good, :_destroy]) end end
quantified.rb
class Quantified :all_blank, :allow_destroy => true #correct scope :averaged, -> { where(categories: 'Averaged') } scope :instance, -> { where(categories: 'Instance') } scope :private_submit, -> { where(private_submit: true) } scope :public_submit, -> { where(private_submit: false) } validates :categories, :metric, presence: true acts_as_taggable CATEGORIES = ['Averaged', 'Instance'] end
result.rb
class Result { where(good: true) } scope :good_count, -> { good.count } end
在尝试为结果引入不同的字体颜色时出现了问题。 为了实现这一点,我不得不引入导致双重渲染的这两行: <div class="">
<div class="">
感谢您的时间和专业知识。
您应该详细了解存储的逻辑和数据。
一个猜测是@averaged_quantifieds
中有两个记录,而@averaged_quantifieds
三个记录。 在不知道存储的数据的情况下很难确定所需的结果。
请注意,您只在<%= raw ...
行中显示第一个记录结果( averaged.results.first
)
尝试
<% @averaged_quantifieds.each do |averaged| %> <%= raw averaged.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %><%= link_to edit_quantified_path(averaged) do %> <%= averaged.results.first.result_value %> <%= averaged.metric %> <%= averaged.results.first.date_value.strftime("%b") %><% end %> <% end %>
编辑:我的不好,我错过了一个隐藏在行的其余部分的result
对象( <%= date_value_label_class(result) %>
)
如果适用,将result
更改为averaged.results.first
<% @averaged_quantifieds.each do |averaged| %> <%= raw averaged.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %><%= link_to edit_quantified_path(averaged) do %> <%= averaged.results.first.result_value %> <%= averaged.metric %> <%= averaged.results.first.date_value.strftime("%b") %><% end %> <% end %>