如何停止双渲染在边栏?(How to stop double rendering in sideba

2019-10-22 13:57发布

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

当它应该只列出它曾经像这样:

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

结果是nested_attribute到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' %>"> <% averaged.results.each do |result| %> <div class="<%= result.good? ? 'green' : 'red' %>">

谢谢您的时间和专业知识。

Answer 1:

你应该更多地解释的逻辑和存储的数据。

一种猜测是,有两条记录@averaged_quantifieds和三个averaged.results 。 这是很难确定所期望的结果而无需知道所存储的数据。

请注意,您只显示首创记录结果( averaged.results.first中) <%= raw ...线

尝试

<% @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) %> )躲在行的其余部分

更改resultaveraged.results.first (如适用)

<% @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 %>


文章来源: How to stop double rendering in sidebar?