How to dynamically style HTML elements in Rails based on an object’s property

I was helping someone with a project and they wanted to change the CSS class of an image based on a property of an object that it was attached to. The person was used to using AngularJS’s ng-class and wanted similar functionality.

For demonstration purposes let’s assume we have an ActiveRecord model named User that has a property named “awesome” which can be true or false. Let’s also assume we have an image named “doggie.jpg” in our app/assets/images folder.

The CSS file.

.awesome-true{
  border: 3px solid green;
}

.awesome-false{
  border: 3px solid red;
}

In the view (@user is a single instance of the User model)

<%= image_tag "doggie.jpg", :class => "awesome-#{@user.awesome}" %>

The result when true.

<img class="awesome-true" src="/assets/doggie-6708b9ff28cb5d2d941ed3b01dfe977c611a0ea07dc04249dbdce4514245c303.jpg" alt="Doggie 6708b9ff28cb5d2d941ed3b01dfe977c611a0ea07dc04249dbdce4514245c303">

The result when false.

<img class="awesome-false" src="/assets/doggie-6708b9ff28cb5d2d941ed3b01dfe977c611a0ea07dc04249dbdce4514245c303.jpg" alt="Doggie 6708b9ff28cb5d2d941ed3b01dfe977c611a0ea07dc04249dbdce4514245c303">

Having someone to compare yourself to

Today is my birthday (yay!) and I feel like dispensing some life advice. Maybe it’s because I have kids or maybe it’s because my day job is being an instructor at a coding bootcamp; personally and professionally I’m constantly thinking about how to properly instruct the people I’ve been given responsibility for.

I got into coding in a really oddball way; I didn’t have any friends who were programmers, I didn’t come from a programming family. The short version of the story is that one day I decided that I wanted to be a programmer and set off on my journey. For about 5 months I studied night after night alone in a room; I didn’t even tell my family my plan.

Looking back on it now if I had to give new programmers some beginning advice I would say find people to compare yourself to. But realize when I say this there is a degree of subtlety that I want to convey.

I DO NOT advocate finding people to compare yourself to for the purpose of trying to compete with them.

I DO advocate finding people to compare yourself to for the purpose of gauging your own ability.

I had my own struggles with “Imposter Syndrome” and now I believe that part of what caused it was an inability to properly gauge my progress. I thought that everyone who I came across was better than me and that I was the worse programmer in the world, even though demonstrably I obviously was not.

Over the past 6 months while being exposed to complete newbs and experienced developers I’m in a better position to gauge my own skill, and that Imposter Syndrome I had has pretty much disappeared and been replaced with a confidence that I thought would take years to develop. I am confident in my problem-solving skills, but also cognizant of my own limitations.

To crystallize this message: Make friends with a few programmers and talk shop with them. Look at their code. Don’t be afraid to ask “stupid” questions, because if there’s anything programmers love it’s an opportunity to prove to someone how much more they know than someone about a subject 🙂 Soon you will have a better sense of your actual skill level, and from there you can create a targeted action plan to improve yourself.