Monday, December 8, 2014

Setting up rails

Unfortunately I only know how to do this on Mac. I have been wanting to set up a Windows development environment for a while, but it just doesn't seem to be worth the time or effort. I have also installed a few other things like git, heroku, and maybe a few other useful things that I can't think of right now. Anyway, this post is not about how to install things. It is a reminder for me of what I should use to keep things updated.

I first learned rails by reading Michael Hartl's tutorial, so just about everything I do here follows his instructions. If you know nothing about rails, then the rest of the blog won't be useful to you.

To set up rails, we must first install Ruby. I use rvm (see page for installation instruction). Once it is installed, keep it updated using

  rvm get stable
and then install the required dependencies for Ruby using (you need to have Xcode installed)
  rvm requirements
There is a cheat sheet here that lists the most important commands. The ones I have used are
  rvm list known
  rvm install 2.1.5
  rvm use --default 2.1.5 
The last line sets it up so that version 2.1.5 is used for current and future sessions.


Installing rails

To install rails, use

  gem install rails 
... and that's it!


Rails Quickstart

To start a new project:

  rails new <appname> --skip-test-unit --skip-bundle
where appname is the name of your application. I skip test-unit because I have never used it, and I prefer to write my own tests ( ... this may not happen in this project).

Once rails create the directory and necessary files, I copy and paste the following files from previous projects:

  1. /.gitignore -- list of files to be ignored by git (e.g. swap files, database.yml)
  2. /config/database.yml -- database setting, modify it after copying
  3. Gemfile -- list of add ons used for the rails project

As you can see, I use GitHub to store my code. To use git, do the necessary installation, then start a repository via the website. Link your local directory using the instruction provided, i.e.

  git init
  git remote add origin <git address>
Here are the commands to add all untracked files, commit, and the push. By the default,
  git add -A
  git commit -m "Initial commit"
  git push origin master
It might be a good idea to run bundle before pushing, just to make sure all the gems work.

For deployment, I use Heroku, since this makes the whole process amazingly simple! You need to create an account, then download Heroku toolbelt from here and install it. Once this is done, link the app to Heroku using

  heroku login
  heroku apps:create  --stack cedar
Heroku creates a remote location named 'heroku' where you can push your code to, so to deploy your app, use
  git push heroku master
It might be a good idea to push to heroku and see if it can be deployed now (even though it won't do anything). To view the site locally, run the rails server using the command
  rails server -p <port number>
and then open the site at http://localhost:<port number>, which should give you a welcome message.

No comments:

Post a Comment