Archive

Archive for December, 2009

On the Rails: Set Up Git Repos Locally and On GitHub

December 31st, 2009 Comments off

GitHub

Let’s prepare GitHub for our code’s arrival.

  1. Go to github.com.
  2. If you don’t already have an account, click on “Pricing and Signup” at the top and get a free one.
  3. Log in to your account.
  4. Click “New Repository” in the upper right of the “Your Repositories” box.
  5. Give your repo the name “railstest”, along with an appropriate description and URL if you know where your project will live in production (i.e. its hostname).
  6. Click “Create Repository”.

Next, we need to generate an SSH keypair on our local machine, and add the public key to our GitHub account so our local machine can access the GitHub repo we just created. In a Terminal, issue the following command:

ssh-keygen -C "youremail@yourdomain.com" -t rsa

Accept the defaults, and don’t supply a passphrase unless you want to. Now, issue the following command to display the public key:

cat ~/.ssh/id_rsa.pub

Copy the contents of ~/.ssh/id_rsa.pub to the clipboard, and follow these steps:

  1. On GitHub, click “Account Settings” in the upper right of the page.
  2. Click “SSH Public Keys” in the second row of tabs.
  3. Click “Add another public key”.
  4. Give the key a title, like “Jeremy’s Key on MacBook”.
  5. Paste the contents of ~/.ssh/id_rsa.pub from the clipboard into the “Key” textarea.
  6. Click the “Add key” button.

Now, we’re ready to set things up locally.

Locally

With a few additions and modifications, we’ll follow the instructions you saw after creating the repo. In Terminal, from your home directory, issue the following commands in order:

git config --global user.name "Your Name"
git config --global user.email youremail@yourdomain.com
cd workspace/railstest
git init
git add .
git commit -m 'Initial import'
git remote add origin git@github.com:your_github_username/railstest.git
git push origin master

Now, let’s go back to our repo on GitHub and make sure our code made it there OK. That’s it!

Next time: We’ll create a Cloud Server on the Rackspace Cloud, set it up to host our app, and use Capistrano to deploy our code from GitHub to our server. See you then!

Categories: On the Ground Tags:

On the Rails: Install Apache 2 and Passenger Locally

December 31st, 2009 Comments off

For the sake of matching our production environment as closely as possible, let’s take a few minutes to install the Apache 2 web server and Phusion Passenger application layer locally. First, Apache 2:

sudo port install apache2

Next, Passenger:

sudo gem install passenger

Now, let’s build and install the Passenger extension module for Apache 2. This part was a little tricky for me, due to the fact that we’ve installed another Apache 2 via MacPorts. We want the Passenger module build process to find the MacPorts one, and not the one that came with Snow Leopard. I found and followed a couple other instructional blog posts on this, but I ended up doing something other than what they did to get it to work.

First, let’s add a line to the end of our ~/.profile file. (Type “pico ~/.profile” to edit it.)

export PATH="/opt/local/apache2/bin:$PATH"

Next, run the following command:

source ~/.profile

Now, we can build the module:

sudo passenger-install-apache2-module

After that builds and installs the module, let’s edit our Apache 2 config file. (Type “sudo pico /opt/local/apache2/conf/httpd.conf” to edit it.)

First, let’s type Ctrl-W and enter “80″ to search for the string “80″. 80 is the default port Apache listens on. But that port is already bound to the Snow Leopard Apache. Let’s change the port our MacPorts Apache will listen on to 3000. Comment out this line (add a “#” character in front):

Listen 80

And add the following line right underneath it:

Listen 3000

Next, let’s add the following to the end of the file:

LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.8
PassengerRuby /opt/local/bin/ruby
PassengerDefaultUser your_username

<VirtualHost *:3000>
   ServerName localhost
   DocumentRoot /Users/your_username/workspace/railstest/public
   RailsBaseURI /
   RailsEnv development
   <Directory /Users/your_username/workspace/railstest/public>
      AllowOverride all
      Options Indexes FollowSymLinks -MultiViews
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>

The Phusion Passenger users guide for Apache was helpful on this step. Be sure to replace “your_username” and “workspace” above with your username and the name of the directory containing your Rails projects.

Now, let’s start Apache:

sudo apachectl start

And, in a separate Terminal, start PostgreSQL if it’s not already running:

sudo su postgres -c '/opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/railstest'

Browse to:

http://localhost:3000/books

and voila! Our CRUD app from last time is now running on Apache with Phusion Passenger instead of WEBrick!

Next up: Git and GitHub stuff.

Categories: On the Ground Tags:

On the Rails: Create a Simple CRUD App, Set Up a Database

December 29th, 2009 Comments off

Once you have everything installed, it’s actually pretty easy to create a basic application in Rails that can perform the four basic CRUD operations — Create, Read, Update, Delete — on a set of data.

Before we create the app, though, let’s briefly describe what our data is going to look like. Let’s say we want to store some basic information about all of the books we have in our possession. Most books have, among other things, a title, an author, a publishing company, a copyright year, and an ISBN number (a unique identifier). If our database table is going to be composed of books, then each record in that table is going to have each one of the above attributes, stored as strings of text.

Create CRUD App

So, let’s first create our initial, empty skeleton of a Rails app.  From your home directory, or whatever directory you want to store your Rails projects in, issue the following command:

rails railstest -d postgresql

This will create a folder named “railstest” and populate it with all of the wonderful goodies that Rails provides as a foundation for writing a web-based application that follows the Model-View-Controller architectural pattern. The “-d postgresql” part pre-configures the app to use the relational database system we have chosen, PostgreSQL. (If you wanted to use MySQL or something else, you could just as easily specify a command-line argument for whatever you’ve chosen instead. Type “rails –help” to see all the options.)

Now, we’re going to take our foundation and build a CRUD app on top of it. But we’re not going to type a single line of Ruby code. We’re going to make use of scaffolding. (Later, we’ll want to avoid scaffolding like the plague, and build our apps by actually writing our own Ruby code, but we’re trying to get up and running ASAP, so we’ll take the easy way for now.)

Issue the following commands from the directory containing your Rails project(s):

cd railstest
./script/generate scaffold book title:string author:string publisher:string copyright_year:string isbn:string

This will create some code files — models, views, and controllers — and a database migration, which describes how to set up our database schema. We’ll be invoking rake (a build program for Ruby) a little later to do just that.

Set Up Database

Before we do that, though, we should create a database cluster, launch the database server, create a new database user, and finally create the database. (You may have taken note of some instructions that appeared in your Terminal after you installed PostgreSQL. If you did, some of the commands in this step should look familiar.)

First, we need to create and initialize the data directory, or database cluster. Issue the following three commands in order:

sudo mkdir -p /opt/local/var/db/postgresql84/railstest
sudo chown postgres:postgres /opt/local/var/db/postgresql84/railstest
sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/railstest'

Next, launch the database server. Open a new Terminal tab or window, and then issue the following command:

sudo su postgres -c '/opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/railstest'

Next, create a new database user (or role):

sudo su postgres -c '/opt/local/lib/postgresql84/bin/createuser -S -d -R railstest'

Finally, create the development database:

sudo su postgres -c '/opt/local/lib/postgresql84/bin/createdb -O railstest railstest_development'

Now, let’s run rake and create our schema:

rake db:migrate

Just Push Play

We’re almost there! Time to start up our web server and Ruby application layer. At the moment, all we have is WEBrick, which comes with Ruby. (Note: WEBrick is a quick and dirty web server solution that will run our simple app and get us to the end of this part of the tutorial, but it should not be used for anything more complicated, and certainly not in a production environment.) Run the following command in a new Terminal:

./script/server

Now, open your web browser and go to:

http://localhost:3000

You should see a Rails welcome screen. Next, go to:

http://localhost:3000/books

You should see our CRUD app! You should now be able to Create, Read (or Show), Update (or Edit) and Delete (or Destroy) books. The data is stored in our PostgreSQL database instance we created above.

Next Episode

Next time, we’ll put the code under version control locally using git, set up a remote repository on GitHub, and then push our code to that remote location. See you then!

Categories: On the Ground Tags:

Setting Up Rails and PostgreSQL on Snow Leopard

December 15th, 2009 Comments off

Hello again. This post will go over how to set up the Ruby language, the Ruby on Rails web framework, and the PostgreSQL relational database server on an Intel Mac running Mac OS X 10.6 (Snow Leopard).

Note: As the Perl folks say, there’s more than one way to do it. This is only one of many possible configurations and ways to set things up. I’m writing this post (and the next two or three) for my future self and anyone else who might want to get up and running with Rails this way.

In the next few posts, we will: create a simple database-driven CRUD web application in Rails; set up a source repository on GitHub and push our code there; create a server on the Rackspace Cloud, set it up for production use, and finally deploy our app from GitHub to that server.

This post is really an adaptation of a nice guide written by Jared McFarland. His setup includes MySQL rather than Postgres, and so my post will only modify and elaborate on his where appropriate.

Let’s begin.

MacPorts

The first thing we want to get is MacPorts, a system by which we can easily get the latest, most widely-used versions of Ruby, RubyGems (a Ruby software packaging system), Ruby on Rails, and PostgreSQL. (Snow Leopard does come with versions of Ruby, Gems and Rails, but things move fast, and they were already outdated when Snow Leopard was released.)

Go here and get the latest DMG disk image for Snow Leopard (1.8.1 at this writing). Install the PKG. (Note: MacPorts will put itself and everything else you install via MacPorts in the /opt folder on your primary hard disk, so you’ll know where everything is in case you want to get rid of it later.)

Ruby

[Update, 12/21/2009, 4:30 p.m. MST: Marc Chung tells me that a better way to install Ruby than that described in this section is via rvm (Ruby Version Manager), which allows you to have multiple Rubies installed and switch between them. More details forthcoming.]

In Terminal, do:

sudo port install ruby

and enter your user account password if/when prompted. This will install the latest Ruby 1.8, which at this writing is 1.8.7, patchlevel 174, dated June 12, 2009.

When it’s done, do:

which ruby

and you should see:

/opt/local/bin/ruby

Now, do:

ruby -v

and you should see:

ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10]

RubyGems

sudo port install rb-rubygems

Latest RubyGems on MacPorts is 1.3.4. (1.3.5 is the actual latest and is coming to MacPorts Real Soon Now).

SVN and Git

sudo port install subversion
sudo port install git-core +svn

If you don’t have any existing Subversion repositories, you can ignore the subversion line and omit “+svn” from the git-core line. If you’re starting from scratch, you only need Git. Latest Git on MacPorts is 1.6.5.3 (actual latest 1.6.5.6).

PostgreSQL

sudo port install postgresql84-server

Latest PostgreSQL is 8.4.2, and it’s on MacPorts.

[Update, 12/24/2009, 12:30 am MST: Add the following line to the end of your ~/.profile file:

export PATH="/opt/local/lib/postgresql84/bin:$PATH"

Then issue the following command from the prompt:

source ~/.profile

The PostgreSQL binaries need to be in your PATH; if they aren't, the next step will fail.]

That’s all for the MacPorts stuff. Now for some Ruby Gems.

PostgreSQL Ruby Adapter

sudo gem install pg

Latest PostgreSQL Ruby Adapter gem (pg, a.k.a. ruby-pg) is 0.8.0.

Rails

sudo gem install rails

Latest Rails is 2.3.5. This includes eight gems — actionmailer, actionpack, activerecord, activeresource, activesupport, and rails (all 2.3.5); rack (1.0.1); and rake (0.8.7).

You now have all the software you need to start developing web applications using Ruby on Rails on your Snow Leopard Mac!

Be sure to tune in next time, for another exciting episode of Jeremy’s Wild and Crazy Adventures on (off?) the Rails, when we’ll discover how to create a basic CRUD app in Rails without writing a single line of Ruby code! (Before long, we’ll want to develop apps by actually writing some Ruby code, but more on that later.) We’ll also set up Git repositories in which to store and track our code revisions, both locally and on GitHub. See you then!

Categories: On the Ground Tags: