Reputation: 2866
When a User checks off :good
in the _form how can we render the font color green of that result in the sidebar?
We made this work in the index: How to Change Font Color based on Conditional?.
But the sidebar logic is making this trickier by rendering an additional quantified/result for every result when I tried this (we try to address this issue furthur here: How to stop double rendering in sidebar?):
<% @averaged_quantifieds.each do |averaged| %>
<% averaged.results.each do |result| %>
<% if result.good == true %>
<div class="green">
<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="label label-info"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
</li>
<% else %>
<div class="red">
<li>
etc...
Current code:
<% @averaged_quantifieds.each do |averaged| %>
<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="label label-info"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
</li>
<% end %>
<% @instance_quantifieds.each do |instance| %>
<li>
<%= instance.results.first.date_value.strftime("%b %d") %>: <%= link_to edit_quantified_path(instance) do %> <%= instance.results.first.result_value %> <%= instance.metric %>, <%= raw instance.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %><% end %>
</li>
<% end %>
application_controlller
def set_stats
@quantifieds = Quantified.joins(:results).all
@averaged_quantifieds = current_user.quantifieds.averaged if current_user
@instance_quantifieds = current_user.quantifieds.instance if current_user
@statsresults = current_user.results.stats if current_user
end
UPDATE
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
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
result model
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
Thank you for your time and expertise.
Upvotes: 1
Views: 60
Reputation: 54882
You can simply do:
<% averaged.results.each do |result| %>
<div class="<%= result.good? ? 'green' : 'red' %>">
In Rails, ActiveRecord will automatically define a method on each boolean fields, method which would always return either true
or false
(never nil
):
# example
# User's boolean attribute 'is_admin' in the DB
# Rails will define an instance method called `is_admin?`:
User.new(is_admin: true).is_admin? # => true
User.new(is_admin: false).is_admin? # => false
User.new(is_admin: nil).is_admin? # => false
# the defined method's name is `<boolean_attribute_name>?`
Also you don't need to be explicit like this:
if result.good == true
#etc.
end
You can just write:
if result.good
#etc.
end
If result.good
returns false
or nil
, it will not go into the if block. If it returns anything else than false
or nil
, it will execute the if block.
Upvotes: 2