In previous Ruby on Ubuntu tutorial, you learned how to install and work with ruby language. Let’s learn Ruby on Rails Framework in this tutorial. I am going to take step by step approach to guide you to the installation and app development in this tutorial.
Phase 1: Installation
Ubuntu comes up with default version of 1.8.7 with LTS 12.04. You have to update your ruby version before installing recent version of rails. You can do that using following steps.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install ruby1.9.3
Note: If you’re reading this tutorial for future version of Ubuntu LTS, adjust the ruby version to the recent update.
Now that you have ruby installed, check to see which is currently set to default.
sudo update-alternatives --config ruby
Manually select the ruby version and check back which is the current running version of the ruby.
ruby -v
ruby 1.9.3p0
Now that you have installed ruby, let’s proceed with installation of ruby version manager (RVM).
sudo apt-get install ruby-rvm
If rvm installs just fine, you can go ahead and install rubygems.
sudo apt-get install rubygems
If the gem installation is succesful you can install rails for sure.
sudo gem install rails -v 3.2.3
It will take some time to install the rails server and once it is completed, you can check the server version.
rails -v
Phase 2: App Development
Let’s create simple app that let’s you bookmark URL. In this application we are going to take a look at three fields – Title, URL and Description.
rails new urlbook
We’ve just created our rails app with this command. Let’s get inside our new app directory.
cd urlbook
Now let’s start the server and see if we can get the default rails page.
rails server
Now let’s build our app with scaffold feature.
rails generate scaffold bookmark Title:string URL:string Description:text
This command will generate our bookmark application. Now let’s connect our app to database using rake.
rake db:migrate
This will by default use the sqlite database. If you wish to use the mysql database or any other database then you have to get edit /config/environments/database.yaml
Now let’s run the server and see if our app is running fine.
Go to http://localhost:3000/bookmarks/
You will see the option to add the url and the respective fields. Add some dummy text and see if that information is added or not. That’s it. You have successfully created and executed your rails app on ubuntu.