ParsaLabs | Blog

A publication about the web and more.

Pow Server

| Comments

I strongly suggest that you use Pow server to run your Rails (or any Rack) applications locally.

It installs very fast, works with rbenv, you don’t need to configure anything and you can run multiple apps at once (each with its own version of Ruby and its own set of gems). This last advantage is particularly great because, in many occasions, you are dealing with multiple apps that are dependent on each other.

Installing pow is a breeze. At a terminal prompt do:

1
curl get.pow.cx | sh

enter your password when prompted, and you are good to go.

Pow runs any rack application (rails included) with a config.ru file. Let’s say we have a rails app under ~/work/bestapp. To run your app with Pow, cd into your app’s directory and install dependencies using bundle install. Then:

1
cd ~/.pow

and symlink your project:

1
ln -s ~/work/bestapp

And now if you browse to http://bestapp.dev, you will be able to see your application running with Pow. Notice that the name of the symlink specifies the hostname you will use to access the application. ~Happy days!

For more info (like restarting apps, serving static assets, viewing logs, etc) visit Pow’s awesome user manual here.

Enjoy, and pass it on!

Installing PostgreSQL on Mac OS X, and Using It as Your Rails App’s Database

| Comments

Installation:

The easiest way, I have found so far, to install PostgreSQL database on Mac OS X is to use the postgres app. Here are the steps:

  • Log on to http://postgresapp.com/.

  • Download the latest version.

  • Unarchive the downloaded zip file (usually by just double clicking on it). This will give you back the Postgres app.

  • Finally, drag and drop the Postgres app to your Applications folder.

Now, simply double click to launch the app (i.e. to run the world’s most advanced open source database on your Mac). It just couldn’t get any easier.

You should be able to see a tiny elephant icon in your menubar. That means your database is up and running.

Tip: I suggest setting up your database to run/start automatically at Login. To do that, click on the elephant icon in your menubar and select ‘Automatically Start at Login’.

Next, you are going to want to add the /bin directory that ships with Postgres app to your PATH. Open your .bash_profile (in your home directory) and add the following line to the bottom of it (if .bash_profile doesn’t exist, simply create it by running touch ~/.bash_profile):

1
PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"

Restart the terminal, and run which psql. You should get the following result:

1
/Applications/Postgres.app/Contents/MacOS/bin/psql

Working directly with the database:

Now if you type psql at a terminal prompt, you will be connected to the database server and will be able to interact with it through various commands. A list of all psql’s commands can be found in this tutorial.

Tip: running rails db command, in the context of your rails app, will do the same thing as psql.

Personally, my preferred way to explore the db structure, tables and data is through a GUI client. At the time of this writing, my favourite client is PG Commander, but other alternatives are pgAdmin and Induction app.

Using Postgres with your Rails app:

Alright, with Postgres app successfully setup, we can now create a new Rails app, and instruct it to use PostgreSQL as database:

1
rails new myapp -d postgresql

When the application is created, Bundler will install the pg gem. Looking at the database.yml file, you can see the application is already configured to use Postgres. However, you need to make some minor changes to make it work with postgres.app. Your configuration should look something like this (for development, as well as, test and production databases):

1
2
3
4
5
development:
  adapter: postgresql
  encoding: unicode
  database: <insert_your_desired_db_name_here>
  host: localhost

Now run:

1
rake db:create:all

at the root of your rails app to create the database.

To verify that your Rails app is able to connect to your newly created database, and is able to interact with it, we are going to create a post scaffold, run the migration, and check if we can create, retrieve, update and delete posts through the browser:

1
2
3
rails g scaffold post title:string body:text
rake db:migrate
rails server

Browse to http://0.0.0.0:3000/posts and try creating some posts. If you can retrieve, update and delete them successfully, you are all set!

Setting Up a Ruby on Rails (4) Development Environment on a Clean Mac OS X Installation

| Comments

First, install the command line tools:

  • Browse to Apple Developer Download Page and sign in with your apple ID.
  • In the list of packages, look for Command Line Tools (OS X Mountain Lion) for Xcode (or whatever your mac version happens to be at the time you are reading this).
  • Download the .dmg package, and install it by double-clicking on it and following the installation instructions.

Install homebrew:

  • Run the following command at a terminal prompt:
1
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
  • Then run brew doctor in terminal to make sure everything is ok with your homebrew installation, in which case, you will see the “Your system is ready to brew” message. Don’t worry if there are any warnings, just read the description carefully, most of the time you will find useful suggestions. Follow them, and you will be fine. During my installation, I got a warning about an outdated version of Git, and as a remedy, homebrew suggested that I run brew install git which solved the problem. Running brew doctor again verified it, and I was good to go.

  • Finally, run brew update

Install Rbenv:

  • Run the following command at a terminal prompt:
1
brew install rbenv
  • You will need to configure your bash profile. You’ll only ever have to do this once. (in case you cannot find your bash profile in the home directory, simply create it by running touch ~/.bash_profile.
1
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Then:

1
source ~/.bash_profile
  • Install ruby-build and rbenv rehash gem using homebrew:
1
2
brew install rbenv-gem-rehash
brew install ruby-build

Note: to later update these installs, use upgrade instead of install.

At this point, we are pretty much done with rbenv installation.

Install Rubies:

  • Now, you can install different versions of ruby using the rbenv install command. For example, below we are going to install 3 versions of ruby:
1
2
3
rbenv install 1.9.3-p194
rbenv install 1.9.3-p429
rbenv install 2.0.0-p353
  • Run rbenv rehash. In fact you should run this every time you install a new version of ruby or when you install a gem that provides a command.

  • Set the 2.0 version as the global default (because it is the latest and greatest ruby, you should all use/prefer it over other versions):

1
rbenv global 2.0.0-p195
  • At this point you should spend some time reading rbenv command reference and get familiarised with different commands that rbenv provides for simple ruby version management. Highly recommended that you read the above link very carefully before moving on to the rest of this tutorial. however, here’s a summary of the most important commands:

rbenv local <VERSION> : will set the local project level version of ruby to the VERSION specified.
rbenv local : will report the local version of ruby currently being used.
rbenv versions : will report all ruby versions known to rbenv.
rbenv version : will report the currently active ruby version.
rbenv which irb : will show the full path to the executable that rbenv will invoke when you run the given command.

Install Gems:

  • Next, we will update to the latest Rubygems version:
1
gem update --system
  • Install the rails 4 gem:
1
gem install rails --version 4.0.0 --no-ri --no-rdoc
  • And other gems required for rails development:
1
gem install bundler foreman pg thin --no-rdoc --no-ri
  • Run rbenv rehash

And we are pretty much done with our basic rails 4 development environment :–)

To test things out:

To ensure everything is installed properly:

1
2
3
rails new my_first_app
cd my_first_app
rails server

and browse to http://0.0.0.0:3000/ in your favourite web browser. You should be able to see the infamous rails welcome page (as depicted below):

It worked! Yaaaay!

Welcome Post

| Comments

Hi everyone-

This weblog has been launched as a medium for sharing our experiences, thoughts and opinions about software, user experience, simplicity, business and more.

Hope you will enjoy reading as much as we enjoy writing it.

Please visit often.

-Pouya Arvandian