rails

Setting up PostgreSQL on Mac OS X

What’s that? You’re making a Rails app, planning on eventually pushing it to Heroku, and you’re still running SQLite locally on your machine? Like a chump!? Come on now!

You’ve got to install Postgres locally! The good news is: It’s super easy.

First Things First

You’ll first need to install PostgreSQL. For this tutorial, we’ll use Homebrew to help us do that quickly.

Open terminal and type: brew -v
This will return the current version of brew (if you have it installed).
If you do have it installed, update it with the following command: brew update

That was pretty simple

If you saw a command not found: brew (or similar) error after you ran brew -v, then you likely need to install Homebrew. If you would like to do that now, type the following into your terminal:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Pro Tip

If at this point you’re thinking of installing Homebrew, consider first reading more about installing Homebrew in this article I wrote on How To Install Homebrew on Mac OS X 10.7+.

With brew installed, you’re golden. Time to install PostgreSQL!

Install PostgreSQL and Configure

With Brew, you can install PostgreSQL with the following command in Terminal:

brew install postgresql

You can now start your PostgreSQL server and create a database:

initdb /usr/local/var/postgres

Optional
You’ll need to have PostgreSQL running locally in order for your app (running in development mode, of course) to read and write to your Postgres database(s). If you want to have PostgreSQL start automatically each time you start your computer, enter the following three lines into Terminal one after another:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.1.3/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Done and done. PostgreSQL is up and running and now all you need to do is tweak a few setting in your Rails App’s database.yml file (in the config/ folder).

In your database.yml file, you’ll see a few environments and their respective configs beneath. Most likely you’ll see three environments: development:, test:, and production:.

For now, we’ll just change the development: environment. If you haven’t changed anything, you’ll see the following as the default config for development::

development:
# adapter: sqlite3
# database: db/development.sqlite3
# pool: 5
# timeout: 5000

In order for your app to use your new PostgreSQL server, you’ll want to change the above to this:

development:
# adapter: postgresql
# database: name_of_your_app_development
# encoding: utf8
# template: template0
# host: localhost

Super-awesome Protip

You’ll want to replace name_of_your_app with the name of your app.

Editing Your Gemfile

Hold on there partner, don’t forget to tweak your Gemfile! Make sure the you’ve got the pg gem in your gemfile:

Example line entry in your Rail's `Gemfile`, if you want to use the `pg` gem:
gem 'pg'

Want To Run PostgreSQL in Production?

If you want to run Postgres in your production environment as well as your development environment, make sure to add the gem 'pg' line somewhere within the :production block—and not only within your group :development, :test do block.

Finally, you’ll want to create a new database: rake db:create and you’ll probably want to run the following command to delete your tables, recreate them, and seed them with any data you may have in your seeds.db file with the following command: rake db:reset

Trying to install PostgreSQL on your Linux machine instead?

My buddy—Eric MacAdie—offers these helpful instructions for setting up a Postgres server for Rails on a Linux machine instead of OS X.

Credit Where Credit Is Due:

Dan Manges is crazy-smart, the CTO of Braintree, and happens to be my mentor while at Code Academy The Starter League. He saved me about three hours of chin-scratching, by teaching me everything below today (in about 15 minutes). Thanks man!
(Probably worth noting that any errors below are courtesy of yours truly—and not Dan :)

Don't forget to rake after deploying to heroku

Deploying your Rails 3 app to heroku (on their Cedar stack)?

Protip

Don’t forget to run this command—ya know, just as you would when first running the app locally on your own machine.
(Might save you 20 minutes of chin-scratching!)

Run a `rake` task on Heroku:
heroku run rake db:migrate

Good times!

Rails' default HTTP methods for button_to and link_to helpers

Here’s a tip when using Rails’ button_to and link_to URL helpers!
Never ever forget these two things:

button_to uses the :POST method by default

Believe me: Memorizing these two simple Rails defaults will save you routing headaches down the road.

Example — Specifying the :GET method:

Let’s say that you’d like to provide your user with a “Cancel” button on a form that redirects her back to the previous page after it’s clicked. (Because you’re nice, you’ll also throw up a warning modal…):

button_to "Cancel / Delete",
:back,
confirm: 'Are you sure you would like to cancel and delete this post?',
disable_with: 'Deleting...'

Guess what? That’s not right! When clicked, that button will either throw a routing error or unintentionally make a post request to one of your routes. Instead, you need to specify that you’d like the button to use the :GET method, instead of its default :POST method. (refer to rule 1 above.)

Here’s the correct code for such a button:

button_to "Cancel / Delete",
:back,
method: :get,
confirm: 'Are you sure you would like to cancel and delete this post?',
disable_with: 'Deleting...'

See how we specify the method in there with method: :get?

Example — Specifying the :POST method:

Want a text link that ends up sending sending a :POST to one of your routes? Simple. Just remember to pass the correct method:

link_to "Click here to submit post.",
posts_url,
method: :post

If you wanted a button to perform the same action, you wouldn’t need to specify the method, as the default method is already :POST:

button_to "Submit Post",
posts_url,
disable_with: "Submitting Post...

Example — Specifying the :DELETE method:

Just as you need to pass in the proper HTTP method into your button_to and link_to helpers if you’d like to use them for the opposite of their default method, so too must you specify the :DELETE method when you’d like to use that instead:

button_to "Cancel / Delete",
:back,
method: :delete,
confirm: "Are you sure you'd like to cancel and delete this post?",
disable_with: "Deleting..."

Code Academy Has Begun!

Remember that one time when I moved to Chicago to start Code Academy The Starter League? Oh wait, that just happened!

Back Story

After graduating from college, I wisely decided to pursue my interests in technology and got a job working at Mahalo in Santa Monica, CA. (Many fun stories on this experience, of course. I’ll save them for future posts—including, “Greg’s guide to getting a job at a startup”. Good times.) After Mahalo was slapped by Google (note to self: avoid building a business entirely reliant on another company for both traffic and revenue) I parted ways and joined the team at Border Stylo.

I absolutely fell in love with startups. I soon realized that I wanted to become more technical myself so that I could start actualizing my own ideas and start a company of my own. Enter Code Academy.

Why Code Academy The Starter League?

Of course, there are many nice, free, online and interactive “teach yourself how to program” resources these days that enable an aspiring developer to teach herself various languages at her own pace, on her own time (not to mention all of the free and paid video courses that have been available for years).

But, I was looking for something that met the following criteria:

  1. I wanted to do this full-time and completely immerse myself without any distractions;
  2. I wanted an environment where I could work alongside others that were equally as dedicated and passionate about becoming developers;
  3. And, I wanted an adventure—I wanted to travel and live somewhere new for a while.
    Code Academy offered my all of these things and more so I jumped on the opportunity, applied for a spot, was accepted (duh), and made it happen. In talking with some of the then-current (now former) students before and while I applying, I was left without any doubt: this was the program for me and would be awesome.

The Focus

The focus over the next 12 weeks (the duration of Code Academy) will be Ruby on Rails, and a healthy dose of HTML, CSS, and Javascript to be dangerous. In talking with folks, it’s clear that many are looking to land a gig at a startup or dev shop once things are done, others are focusing on an existing idea for a web app that they came into the program with already and will pour everything they learn into that one idea, while many others are somewhere in between.

Code Academy offered a design class this quarter and so we’ll also be working closely with them throughout on the various projects we create throughout, hackathons, demo-day projects, etc.

The Beginning

Things have only just begun and so there’s not too much to share yet. But count on the fact that I’ll be posting regular updates on my progress, funny anecdotes, and everything in between in the coming weeks and months while I’m in Chicago.