ruby on rails - How to use nested_attribute boolean to change font color in sidebar? -


when user checks off :good in _form how can render font color green of result in sidebar?

we made work in index: how change font color based on conditional?.

but sidebar logic making trickier rendering additional quantified/result every result when tried (we try address issue furthur here: how stop double rendering in sidebar?):

<% @averaged_quantifieds.each |averaged| %>   <% averaged.results.each |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) %> <%= 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 |averaged| %>   <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="label label-info"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>   </li> <% end %>  <% @instance_quantifieds.each |instance| %>   <li>     <%= instance.results.first.date_value.strftime("%b %d") %>: <%= link_to edit_quantified_path(instance) %> <%= 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 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

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 time , expertise.

you can do:

<% averaged.results.each |result| %>   <div class="<%= result.good? ? 'green' : 'red' %>"> 

in rails, activerecord automatically define method on each boolean fields, method return either true or false (never nil):

# example # user's boolean attribute 'is_admin' in db # rails define 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 # defined method's name `<boolean_attribute_name>?` 

also don't need explicit this:

if result.good == true   #etc. end 

you can write:

if result.good   #etc. end 

if result.good returns false or nil, not go if block. if returns else false or nil, execute if block.


Popular posts from this blog