How to DRY interface / base constants

Let's say I have a constant ANIMALS = %w(dog cat horse)

. What is the ideal way to give it scripts to avoid duplicating it there? One way to do this is to assign it to the global view, like this:

<script>
  Constants.animals = <%=raw ANIMALS %>
</script>

      

but is there a better way to do this?

+3


source to share


1 answer


I always put my constants in the model it belongs to. This is how I would do something like this:

class Animal
    ANIMAL_TYPES = ['dog', 'cat', 'horse']
end

      



Now I can use this constant anywhere including this

<script>
    Constants.animals = <%=raw Animal::ANIMAL_TYPES %>
</script>

      

0


source







All Articles