Normally, when creating applications and deploying them to the cloud you'd spend a great deal of time setting up servers, configuring them with your trusted Linux® distribution, installing the necessary runtimes, installing and configuring databases, dealing with library dependencies and securing servers. With IBM Bluemix, however, you can simply focus on writing the code, and deployment can take as little as a few seconds.
“Building this simple blog will show you how easy it is to create and deploy a Rails app on Bluemix.”
In this article, we'll build a bare-bones blog as a sample application to showcase the process of getting started with IBM Bluemix and Ruby on Rails. We'll walk you through the steps to create a Rails app, create a SQLDB database (a Bluemix service powered by IBM DB2), connect the app to the database, then deploy and run the app. Building this simple blog demonstrates how easy it is to create and deploy a Rails app on Bluemix.
What you will need
- A Bluemix account. Sign up for a free trial.
- Ruby 1.8.7 – 2.0.0 (supported by Bluemix)
- Rails framework 3.2.13 (supported by Bluemix and the ibm_db gem)
- ibm_db gem
- Cloud Foundry CLI tool
- A text editor/IDE, such as Sublime Text
For this application, we will use Ruby 1.9.3 and Rails 3.2.13. Before getting started with the following instructions, make sure these are installed on your local development machine.
Familiarity with the Ruby programming language and the Ruby on Rails framework is assumed and beneficial, but not strictly necessary to follow along in this article.
Step 1. Create a Ruby on Rails application
- Open a command prompt and create a new Rails app, in our case,
$ rails new Blog
If you have multiple versions of Rails installed you can specify the version we employ here:
$ rails _3.2.13_ new Blog
- As a quick safety test, open another command prompt and start the
Rails server. Remember to run the following command from within the
application directory that was created by the
rails newcommand, in this case,
Blog. This will start WEBrick in development mode, which will now accept HTTP requests on port 3000.
$ rails s
- Access http://localhost:3000 within your browser to make a request to the local web server that we just started. You should see a page similar to the one shown below. This confirms that the app is running correctly on the server. You can now safely stop the server (by pressing CTRL+C, for example).
Step 2. Create models, views, and controllers
For this application we will generate a scaffold, which is a starter template provided by Rails that bundles a model, a controller, and the relevant views for our blog posts.
- Create a new scaffold using the following command. Each post will have
a title and a body of text, so our
Postmodel will have two fields,
body, respectively, represented in the database as a
$ rails generate scaffold Post title:string body:text
The following figure shows a series of files automatically generated by the command, including a migration file within
db/migratethat contains the information necessary to create the table in the database according to the fields we just specified.
- After generating the scaffold we need to create the actual
Poststable in our development database. Upholding its principle of "Convention over Configuration," the Rails framework has pre-configured for us, by default, an SQLite database. To create the table, we simply need to run the migration files. You can do this with the following command:
$ rake db:migrate
Rake is a task management tool widely adopted by the Rails framework. Each Rails project has a series of built-in rake tasks. In this case, we simply ran the database migrations to create the
Step 3. Edit the Rails application
- First, we'll change the home page of our app. With an editor, go to
config/routes.rband add the following code to inform Rails that the
/path of the application should be handled by the
indexaction of the
root :to => 'posts#index'
- Next, go to the public folder and rename the index.html file to something else, like index_.html, or simply delete the file so that it isn't displayed by the web server.
- Now start the Rails server again to see the changes, after which you can stop the server.
Step 4. Create a Ruby on Rails application on Bluemix
- Visit the Bluemix website and log in. Create a free account if you don't already have one.
- After you log in you'll be redirected to a Dashboard. Within it you'll see an Applications section and a Create an Application link. Click this link.
- In the Catalog that appears you'll have a lot of components to choose
from. Typically, you will select a runtime of your choice and then add
components and services as needed.
In this case, we are interested in creating and deploying a Rails application on Bluemix, so select the Ruby on Rails runtime, choose a
Namefor your application and a
Hostname, then click CREATE.
The host name must be unique across the entire Bluemix ecosystem, so you may have to get creative. But your application will then have a unique URL that's ready to be shared.
After you click CREATE, you'll have a sample Rails application running on Bluemix, which will be shown in your Dashboard. Clicking the app's link gives you access to the sample application running in the cloud.
- We want our Bluemix application to store post information, so we'll need to create a database to persist that information. For this application, we'll use the SQLDB service, which is based on IBM DB2. Go to your Dashboard/Services and click Add a Service.
- Under the Data Management category, click SQLDB.
Choose the Rails application you created, specify an arbitrary name
for your service, (
blogSQLDBhere), then click CREATE.
When prompted to restart your application, click OK.
- If you visited the URL of the Bluemix app you just created you may
have noticed a page greeting you. The application showing you this
welcome message is a sample app provided by Bluemix the moment you
selected the Rails runtime. This Bluemix sample app is now happily
running in the cloud. We've even added a database service to it.
But it's not our Blog application. We only care about this sample app because it includes an important file named manifest.yml, which specifies the information needed to deploy our application to Bluemix.
The game plan here is to grab this file from the sample Bluemix app and use it within our local Blog app. This way, we'll be able to push our application to Bluemix instead of continuing to run the sample application currently running on Bluemix.
Click on your newly created application tile under the Applications section of your dashboard. From there you should be able to click the VIEW QUICK START button in the top right of the page. A get started window will appear, as shown in the following figure. Click Download the starter application package.
- Extract the package, copy the manifest.yml file, and then paste it
into your local blog application directory.
As mentioned, the manifest.yml file is used by Bluemix when deploying an application. It provides information about added services, such as host, number of instances, and so on.
Note: Another option for getting the manifest.yml file is to download it from our repository on JazzHub. If you do, remember to change the service name, host, and other information to be relevant to your own application.
Step 5. Prepare the Blog app for deployment on Bluemix
- Because our application uses the SQLDB service powered by IBM DB2, our
local Blog application must be able to communicate with DB2. To do
this you must include the ibm_db adapter by adding
gem 'ibm_db'in the Gemfile of your project.
Comment out or delete the line for
gem 'sqlite3'because we are not going to use it in our Bluemix deployment. Set the Ruby version by including
ruby '1.9.3'(or whatever version you are using) near the top of the file.
- Run the following command to install the gem within your local
$ bundle install
Note: After installing the ibm_db adapter (Ruby gem) the app will not be able to start the rails server and run locally unless we have DB2 set up and configured locally, or change the database to SQLite again (in development mode only, for example).
- Now let's change the settings of the production environment. Go to
config/environments/production.rb and set two asset-related
configuration parameters to true:
config.assets.compile = true config.serve_static_assets = true
- Setting up a connection to the database (database.yml) is an important
step when deploying a Rails application. However, when deploying a
Ruby on Rails app on Bluemix, the database.yml file is re-written and
set up to read from the
VCAP_SERVICESvariables automatically, so you don't need to set up the connection for your database service.
This is one example of how Bluemix makes development faster by simplifying the way components and services interact and communicate with each other.
Step 6. Deploy the app on Bluemix
Open a command prompt and run the following
cf commands. If
you haven't installed the cf command line yet, you can do so from the
Get Started with <appname> menu mentioned
- First, let's connect to Bluemix:
$ cf api https://api.ng.bluemix.net
- Then, log into Bluemix:
$ cf login -u <user.email>
Enter your password when requested. Your email and password are the ones you used when registering with Bluemix.
- Create the tables on the SQLDB database by running migrations in the
Bluemix environment. The
-coption allows us to run a command right after our code is uploaded. In this case, we are migrating our database.
$ cf push <appname> -c "bundle exec rake db:migrate"
Tip: When deploying an application open another command prompt and use the following
cfcommand to tail the logs of the application while it is being pushed.
$ cf logs <appname>
Note: After running the previous
cf push, the application will keep trying to connect to our application server but will fail because we haven't started it yet; we've simply run migrations. Check the logs to make sure the tables have been created, then you can cancel the start operation.
- Now that the migrations have run you can deploy your application:
$ cf push <appname> -c "null"
cfcommand-line tool stores the last command passed to
-cand uses it every time we deploy our application. By passing
-cwe can clear the previous command and use the buildpack's default start script to run our application server.
- Congratulations, you now have a Ruby on Rails application running on Bluemix!
- View the application in your browser by clicking your app's link on Bluemix.
If you've followed the steps so far, you'll notice two major differences between this image and the earlier image. One is that we created some sample posts. The second is that we added some web page design elements with HTML/CSS to make the basic application look a little neater. If you checkout the code from our repository, you'll see the actual code that produces the slightly less bare-bone page above.
- If you are interested in reviewing the tables and data contained
within the SQLDB service running on Bluemix, go to Dashboard/Services,
click on your SQLDB service, launch the console, then click
Database Viewer. In Schemas, choose the one that
starts with the letter U (
U71290, for example), which is the user name of the service. In Tables, click POSTS, then click Sample Data to see the data.
Let's quickly recap what we've done.
We developed a local Rails application and tailored it to our preferences. We created a Bluemix application and added a database service from those offered within the current, and ever-expanding, catalog. We also used the sample Bluemix app's pre-configured manifest.yml file to deploy (with the cf command-line tool) our own local app to Bluemix, replacing the sample app that loads by default.
This basic blog application is not all that interesting, or useful for that matter. But what is truly interesting is that with just a few commands, the simple Rails application we developed was successfully deployed on Bluemix and made capable of using the SQLDB service to store and retrieve post information.
You can use this same approach to deploy any Rails application you might develop, as complex as you wish, and employ as many Bluemix components and services as you need.
You'll save time and ease deployment by taking advantage of these enterprise-grade environments, fully managed by IBM and powered by IBM SoftLayer's Cloud. Whether you are using Rails to write a quick proof-of-concept or creating a mission-critical application, Bluemix has got you covered.