Follow Along as I Stumble on the Path to Learning RoR


Make YouTube Embeds Responsive

I have a YouTube video that I embed on my website which I wanted to make responsive.  I found different ways to do it, but by far the easiest was Embed Responsively.  With this, you just paste in the video URL and it creates the code (including CSS) to make the video responsive.

Rails 4.2 Paperclip S3 Tutorial

Getting Paperclip to work on Rails 4.2 while storing files on AWS S3 takes some work on the AWS side, as well as in your app.  Here’s how to do it.

First, prepare S3.

In AWS, go to IAM to create a new user.

ScreenHunter_124 Dec. 13 09.59.jpg

You want to create a user just for your app as opposed to using the master keys for your account so you can limit the access in case someone gets your keys from your app.

Create a new user and enter a name:

ScreenHunter_126 Dec. 13 10.02

After you create the user, you will get this screen.  You will need these keys later, so click the button to download credentials.  Put them somewhere safe because you may need them in the future, too.

ScreenHunter_126 Dec. 13 10.03

Go back to the users view and click on the user

ScreenHunter_126 Dec. 13 10.06

Copy the User ARN value

ScreenHunter_126 Dec. 13 10.07.jpg

Now go to S3.  Create a bucket and note the region you are using.

ScreenHunter_126 Dec. 13 10.09.jpg

Go to the Properties of your Bucket, and click “Add bucket policy”:

ScreenHunter_126 Dec. 13 10.10

Update this code for your User ARN and your bucket name

“Version”: “2012-10-17”,
“Statement”: [
“Sid”: “”,
“Effect”: “Allow”,
“Principal”: {
“AWS”: “your-user-arn”
“Action”: “s3:*”,
“Resource”: [

and paste it in your bucket policy (and save):

ScreenHunter_126 Dec. 13 10.14.jpg

AWS is now set-up.  It’s time to make the changes to your app.

Install the gems in your gemfile:

gem paperclip
gem ‘aws-sdk’

Adjust your models per the examples in Paperclip’s documentation.

Create a s3.yml file in the config folder (you may also want to add this to your .gitignore file to limit the exposure of your keys).  Put your keys from the user there:

access_key_id: “your-access-key”
secret_access_key: “your-secret-access-key”

access_key_id: “your-access-key”
secret_access_key: “your-secret-access-key”

in environments, change the development.rb and production.rb files:

config.paperclip_defaults = {
:storage => :s3,
:bucket => ‘your-bucket-name’,
:region => ‘your-region’

Your region name is not the same as the region you put here.  Find your region here.  If your bucket is not in the US, you may have to do additional settings.

Adjust your model:

has_attached_file :image,
:storage => :s3,
:s3_credentials => “#{Rails.root.to_s}/config/s3.yml”,
:s3_protocol => ‘https’,
:url =>’:s3_domain_url’,
:path => ‘/:class/:attachment/:id_partition/:style/:filename’
styles: {
medium: “300×300>”,
thumb: “100×100>” },
default_url: “/images/:style/missing.png”

To show the image in a view:

<%= image_tag @user.image.url(:thumb) %>


Select Drop Down in Simple_Form and Rails

I’m using the simple_form gem for forms in Rails and I needed to create a select (drop down) for a field.  In my case, it was for selecting US States, but this can be changed easily to whatever options you want to present by changing the options in the list.

In my user.rb model, I put the options (change these if your select is for something else than US States).

[‘Alabama’, ‘AL’],
[‘Alaska’, ‘AK’],
[‘Arizona’, ‘AZ’],
[‘Arkansas’, ‘AR’],
[‘California’, ‘CA’],
[‘Colorado’, ‘CO’],
[‘Connecticut’, ‘CT’],
[‘Delaware’, ‘DE’],
[‘District of Columbia’, ‘DC’],
[‘Florida’, ‘FL’],
[‘Georgia’, ‘GA’],
[‘Hawaii’, ‘HI’],
[‘Idaho’, ‘ID’],
[‘Illinois’, ‘IL’],
[‘Indiana’, ‘IN’],
[‘Iowa’, ‘IA’],
[‘Kansas’, ‘KS’],
[‘Kentucky’, ‘KY’],
[‘Louisiana’, ‘LA’],
[‘Maine’, ‘ME’],
[‘Maryland’, ‘MD’],
[‘Massachusetts’, ‘MA’],
[‘Michigan’, ‘MI’],
[‘Minnesota’, ‘MN’],
[‘Mississippi’, ‘MS’],
[‘Missouri’, ‘MO’],
[‘Montana’, ‘MT’],
[‘Nebraska’, ‘NE’],
[‘Nevada’, ‘NV’],
[‘New Hampshire’, ‘NH’],
[‘New Jersey’, ‘NJ’],
[‘New Mexico’, ‘NM’],
[‘New York’, ‘NY’],
[‘North Carolina’, ‘NC’],
[‘North Dakota’, ‘ND’],
[‘Ohio’, ‘OH’],
[‘Oklahoma’, ‘OK’],
[‘Oregon’, ‘OR’],
[‘Pennsylvania’, ‘PA’],
[‘Puerto Rico’, ‘PR’],
[‘Rhode Island’, ‘RI’],
[‘South Carolina’, ‘SC’],
[‘South Dakota’, ‘SD’],
[‘Tennessee’, ‘TN’],
[‘Texas’, ‘TX’],
[‘Utah’, ‘UT’],
[‘Vermont’, ‘VT’],
[‘Virginia’, ‘VA’],
[‘Washington’, ‘WA’],
[‘West Virginia’, ‘WV’],
[‘Wisconsin’, ‘WI’],
[‘Wyoming’, ‘WY’]

Now in my simple_form, I put this:

<%= simple_form_for(@user) do |f| %>
<%= f.input :indiv_state, collection: User::STATES %>
<%= f.button :submit %>
<% end %>

Changing Column to Add Precision and Scale

If you have to add precision and scale to a decimal field, here’s a sample migration:


class ChangeScale < ActiveRecord::Migration
def self.up
change_column :shirts, :price, :decimal, precision: 5, scale: 2

def self.down
change_column :shirts, :price, :decimal, :precision, :scale

Creating a CSR on Windows

In order to create a SSL Certificate, you need to generate a CSR, or Certificate Signing Request.  I ran into some issues when generating one from Windows, so I wanted to document the steps that I took.

  1. went to to download Win32 OpenSSL v1.0.0k
  2. installed OpenSSL
  3. I received an error regarding C++ during the install, so I installed Visual C++ 2008 Redistributables from the same page I downloaded OpenSSL
  4. Run a command prompt (cmd.exe from the search box in the Windows start menu)
  5. When I tried to run an OpenSSL command, I received an error that a file could not be found (WARNING: CAN’T OPEN CONFIG FILE: /USR/LOCAL/SSL/OPENSSL.CNF)
  6. As noted here, you have to set the environmental variable by typing this into the cmd: set OPENSSL_CONF=c:\[PATH TO YOUR OPENSSL DIRECTORY]\bin\openssl.cfg
  7. navigate to the bin directory
  8. enter your openssl command to generate your csr.  For me, it was:
    openssl req -nodes -newkey rsa:2048 -keyout myserver.key -out myserver.csr
  9. this created key and csr files I could use for creating my SSL certificate

Error From Coffee Files

When I had to set up a new Windows PC with Rails, I ran in to a mysterious error where I couldn’t view my app because of purported problems with my .coffee.js files.  However, I knew there was no issue with the files since they were created during scaffolding and had only comments in them.  Furthermore, I was able to run the same code on my other PC fine, as is.

When I viewed the –trace there was nothing of note (except listing that the coffee files had issues with them).  When I tried to load the view, I did see this though “ExecJS::RuntimeError in Home#index”

Fortunately, I found the solution in this thread which explains that this is the result of Node.js not on a PC, or the path not set correctly in Windows environment variables.  I downloaded the Windows installer (.msi) file and installed Node.js and added the path to the PATH environment variable.  After I restarted my Rails server, the error went away.

Editing Hosts File as Administrator on Windows

I was having an issue editing the hosts file on Windows.  I was receiving an error saying that only the Administrator could edit the file since the etc directory was protected.  My user has Administrator access so I couldn’t understand what was going on.  I tried to change the settings on the etc directory and could not do that either.

So I ended up opening Wordpad (which I use to edit the hosts file), by right-clicking and selecting Run as Administrator.  Once I did that and opened my hosts file in Wordpad, I was able to save the file.