Embed ERB Ruby Code in CSS in Rails

Did you know that you can embed erb code into CSS in rails? I just learned it, and its awesome, and simple.

Start by adding the .erb extension to your css file:

pages.css.erb

Then bam! you can put erb in your css code:

.navbar-brand {
background-image: url(<%= asset_path 'new-logo.png' %>);
background-size: contain;
background-repeat: no-repeat;
}

Advertisements

Initialize Rails App Databases Using MySQL After Cloning Repository From Github

So I cloned my colleague’s rails app from github and wanted to run it on my machine so I can collaborate on the code.  But I kept getting the error that the database didn’t exist.

ActiveRecord::NoDatabaseError (Unknown database 'owl_development')

First, I checked what database this rails app was using by looking at the database.yml file and found it was using mysql and the mysql2 gem.

So I made sure that I had mysql installed on my system by using homebrew and the brew install mysql command.  Then I tried rake db:migrate and still got the error that the database didn’t exist.

What I had forgotten to do after I cloned the repo was run

rake db:create

to create the databases.  I know this is obvious in hindsight, but I spent like 3 hours searching around until I figured out that’s what I had forgotten.  Hopefully this will save you some time!

partial credit here

Play Audio File on Button Click in Rails

So you want to play an audio clip on a button click.  In your view file

<%= audio_tag "audio.m4a", class: "audio-play" %>
<p class="btn btn-primary audioButton">Play</p>

This uses the rails audio tag, and then places a button next to it. In the javascript, you play the audio file on the button click:

$(".audioButton").on("click", function() {
  $(".audio-play")[0].currentTime = 0;
  return $(".audio-play")[0].play();
});

Or in coffeescript:

$(".audioButton").on "click", ->
  $(".audio-play")[0].currentTime = 0
  $(".audio-play")[0].play()

Some credit to this Stack Overflow question.