【问题标题】:How to stop double rendering in sidebar?如何停止侧边栏中的双重渲染?
【发布时间】:2015-04-09 22:10:00
【问题描述】:

代码是这样的双重渲染:

什么时候应该像这样列出一次:

Ran 1 miles Apr 
Journal 1 days Apr

views/layouts/_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 的嵌套属性。

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

在尝试为结果引入不同的字体颜色时出现此问题。为了实现这一点,我不得不介绍导致双重渲染的这两行:&lt;% averaged.results.each do |result| %&gt; &lt;div class="&lt;%= result.good? ? 'green' : 'red' %&gt;"&gt;

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

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller rendering


    【解决方案1】:

    您应该详细说明逻辑和存储的数据。

    猜测@averaged_quantifieds 中有两条记录,averaged.results 中有 3 条记录。在不知道存储的数据的情况下很难确定所需的结果。

    请注意,您仅在 &lt;%= raw ... 行中显示第一个记录结果 (averaged.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(result) %>"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
        </li>
      </div>
    
    <% end %>
    

    编辑:我的错,我错过了隐藏在线路其余部分的 result 对象 (&lt;%= date_value_label_class(result) %&gt;)

    在适用的情况下将result 更改为averaged.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 %>
    

    【讨论】:

    • 如果我正确理解您的问题,则存储的数据是两个平均量化数据和三个平均量化结果。
    • 你的代码给了我:undefined local variable or method 'result'Pages#home 中的 NameError
    • 是的,我试图只显示每个量化的第一个结果。很抱歉您一直在编辑多个 cmets :)
    • 注意 div 行也发生了变化。它有averaged.results.first。我认为您只删除了将result.good? 更改为averaged.results.first.good? 所需的循环
    • 随时。只需快速说明,当您收到有关对象的错误时,请找到它并检查它的来源。为了显示正确的数据,请尝试在控制台中打印值以可视化并查看带来的数据。这将帮助您更快地调试。祝你好运
    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多