ruby on rails - How to stop double rendering in sidebar? -
the code double rendering so:
when should list out once so:
ran 1 miles apr journal 1 days apr
views/layouts/_stats.html.erb
<% @averaged_quantifieds.each |averaged| %> <% averaged.results.each |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) %> <%= 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
results 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 created' else @feed_items = [] render 'pages/home' end end def update if @quantified.update(quantified_params) redirect_to quantifieds_url, notice: 'goal 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 edit 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
the issue came when trying introduce differing font colors results. make possible had introduce these 2 lines causing double rendering: <% averaged.results.each |result| %> <div class="<%= result.good? ? 'green' : 'red' %>">
thank time , expertise.
you should explain more logic , data stored.
a guess there's 2 records in @averaged_quantifieds
, 3 in averaged.results
. hard determine desired results without knowing data stored.
note displaying firsts records results (averaged.results.first
) in <%= raw ...
line
try
<% @averaged_quantifieds.each |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) %> <%= 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 %>
edit: bad, missed 1 result
object (<%= date_value_label_class(result) %>
) hiding in rest of line
change result
averaged.results.first
wherever applicable
<% @averaged_quantifieds.each |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) %> <%= 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 %>