Home Change class color based on enum
Reply: 0

Change class color based on enum

user1513 Published in May 25, 2018, 8:59 am

I have a Workout model that looks like this:

class Workout < ApplicationRecord
  belongs_to :user

  validates :date, :kind, presence: true
  validates :template, inclusion: { in: [true, false] }

  enum kind: { other: 0, swim: 1, bike: 2, run: 3 }

  has_many :comments, as: :commentable

In my view, I iterate through all workouts like this:

      = day.to_formatted_s(:short)
      = link_to 'Add +', new_team_user_workout_path(date: day, athlete: @athlete)
  - unless @workouts[day].blank?
    - @workouts[day].each do |workout|
      = link_to team_user_workout_path(@team, current_user, workout, athlete: params[:athlete]) do
            = workout.kind.capitalize
          - unless workout.distance.blank?
              | Distance:
              | &nbsp;
              = workout.distance/1000
              | &nbsp;km
          - unless workout.duration.blank?
              | Duration:
              | &nbsp;
              = workout.duration/60
              | &nbsp;mins

If enum is 'swim' I'd like to add a class to Workout like this: Workout.swim. If enum is 'bike' the Workout.bike class should be appended, so I can change the background color.

What is best practice for doing this in Rails?


You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318607 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO