如何在侧边栏中停止双重渲染? [英] How to stop double rendering in sidebar?

查看:77
本文介绍了如何在侧边栏中停止双重渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是双重渲染的,就像这样:

The code is double rendering like so:

何时应该将其列出一次,如下所示:

when it should just list it out once like so:

Ran 1 miles Apr 
Journal 1 days Apr

视图/布局/_stats.html.erb

<% @averaged_quantifieds.each do |averaged| %>
<% averaged.results.each do |result| %>
  <div class="<%= result.good? ? 'green' : 'red' %>">
    <li>
      <%= 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 %> <span class="<%= date_value_label_class(result) %>"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
    </li>
  </div>
<% end %>
<% end %>

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.

Results are a nested_attribute to Quantifieds.

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 < ActiveRecord::Base
        belongs_to :user
        has_many :results #correct
        has_many :comments, as: :commentable
        accepts_nested_attributes_for :results, :reject_if => :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 < ActiveRecord::Base
        belongs_to :user
      belongs_to :quantified
        has_many :comments, as: :commentable
      default_scope { order('date_value DESC') }
        scope :good, -> { where(good: true) }
        scope :good_count, -> { good.count }
    end

当尝试为结果引入不同的字体颜色时,就会出现此问题.为了使之成为可能,我必须介绍引起双重渲染的这两行:<% averaged.results.each do |result| %> <div class="<%= result.good? ? 'green' : 'red' %>">

The issue came about when trying to introduce differing font colors for results. To make that possible I had to introduce these two lines which are causing the double rendering: <% averaged.results.each do |result| %> <div class="<%= result.good? ? 'green' : 'red' %>">

感谢您的时间和专业知识.

Thank you for your time and expertise.

推荐答案

您应该详细说明有关逻辑和存储的数据.

You should explain more about the logic and the data stored.

一个猜测是,在@averaged_quantifieds中有两个记录,在averaged.results中有三个记录.在不知道存储的数据的情况下,很难确定期望的结果.

A guess is that there's two records in @averaged_quantifieds and three in averaged.results. It is hard to determine the desired results without knowing the data stored.

请注意,您仅在<%= raw ...行中显示首个记录结果(averaged.results.first)

Note that you are only displaying the firsts records results (averaged.results.first) in the <%= raw ... line

尝试

<% @averaged_quantifieds.each do |averaged| %>

  <div class="<%= averaged.results.first.good? ? 'green' : 'red' %>">
    <li>
      <%= 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 %> <span class="<%= date_value_label_class(result) %>"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
    </li>
  </div>

<% end %>

很糟糕,我错过了一个result对象(<%= date_value_label_class(result) %>),它隐藏在行的其余部分

My bad, I missed one result object (<%= date_value_label_class(result) %>) hiding in the rest of the line

在适用的情况下将result更改为averaged.results.first

Change result to averaged.results.first wherever applicable

<% @averaged_quantifieds.each do |averaged| %>
  <div class="<%= averaged.results.first.good? ? 'green' : 'red' %>">
    <li>
      <%= 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 %> <span class="<%= date_value_label_class(averaged.results.first) %>"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
    </li>
  </div>
<% end %>

这篇关于如何在侧边栏中停止双重渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆