IBM BluemixDevelop in the cloud at the click of a button!

Build a blog app with Ruby on Rails and Bluemix

14 August 2014
PDF (991 KB)
Photo of Lucas Sousa Lima

Lucas Sousa Lima

Software Developer, Big Data University


Photo of Thiago de Aguiar Leal Domingues

Thiago de Aguiar Leal Domingues

Software Developer, Big Data University

@thiagoald on Twitter

Photo of Luiz Gustavo Ferraz Aoqui

Luiz Gustavo Ferraz Aoqui

Software Developer, IBM

@luiz_aoqui on Twitter

Photo of Antonio Cangiano

Antonio Cangiano

Software developer and technical evangelist, IBM

@acangiano on Twitter

Photo of Lucas Sousa Lima

Lucas Sousa Lima

Software Developer, Big Data University


Sign up for IBM Bluemix
This cloud platform is stocked with free services, runtimes, and infrastructure to help you quickly build and deploy your next mobile or web application.

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

  1. Open a command prompt and create a new Rails app, in our case, Blog:
    $ rails new Blog
    Creating a Rails application

    If you have multiple versions of Rails installed you can specify the version we employ here:

    $ rails _3.2.13_ new Blog
  2. 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 new command, in this case, Blog. This will start WEBrick in development mode, which will now accept HTTP requests on port 3000.
    $ rails s
    Starting a Rails server
  3. 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). Rails's default index page

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.

  1. Create a new scaffold using the following command. Each post will have a title and a body of text, so our Post model will have two fields, title and body, respectively, represented in the database as a string and a text column.
    $ 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/migrate that contains the information necessary to create the table in the database according to the fields we just specified.

    Generating Rails scaffold
  2. After generating the scaffold we need to create the actual Posts table 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 Posts table.

Step 3. Edit the Rails application

  1. First, we'll change the home page of our app. With an editor, go to config/routes.rb and add the following code to inform Rails that the / path of the application should be handled by the index action of the posts controller:
    root :to => 'posts#index'
    Editing routes.rb
  2. 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.
  3. Now start the Rails server again to see the changes, after which you can stop the server. Scaffold's default interface

Step 4. Create a Ruby on Rails application on Bluemix

  1. Visit the Bluemix website and log in. Create a free account if you don't already have one.
  2. After you log in you'll be redirected to a Dashboard. Within it you'll see an Applications section and a Create an App link. Click this link.
  3. You will be presented with two options regarding the kind of application you are creating. Select Web.
  4. The next step is to select which programming language we want to use. Because we are building a Ruby on Rails web application, click the Ruby option. You will see some details about the environment. Click Continue.
  5. Now it's time to name your application. Choose a name for your Blog and click Finish. The name will also be used as your application's hostname, which 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 Finish, Bluemix will start deploying your application. After the icon turns green, you'll have a sample Rails application running on Bluemix. Clicking the app's link gives you access to the sample application running in the cloud.

  6. 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 or API.
  7. Under the Data Management category, click SQLDB. Choose the Rails application you created, specify an arbitrary name for your service, (blogSQLDB here), then select the Free Beta plan. As your application grows you will need a larger database, so a different plan might be better. But for now the Free Beta is enough for our sample app. After you are done, click Create. Adding an SQLDB database on Bluemix

    Click to see larger image

    When prompted to restage your application, click Restage.

  8. 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 Ruby 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 Start Coding link on the left side of the page. A get started window will appear, as shown in the following figure. Click Download Starter Code.

    Getting started with a Bluemix application
  9. Extract the package, copy the manifest.yml file, and then paste it into your local blog application directory. Adding a manifest.yml file to a Rails application

    As mentioned, the manifest.yml file is used by Bluemix when deploying an application. It provides information about added services, host, number of instances, and so on.

    manifest.yml content

    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

  1. 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.

    Editing Gemfile
  2. Run the following command to install the gem within your local development environment:
    $ 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).

  3. 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
    Editing the production.rb file
  4. 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_SERVICES variables 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 Start Coding menu mentioned earlier.

  1. First, let's connect to Bluemix:
    $ cf api
  2. Then, log into Bluemix:
    $ cf login -u <>

    Enter your password when requested. Your email and password are the ones you used when registering with Bluemix.

  3. Create the tables on the SQLDB database by running migrations in the Bluemix environment. The -c option 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 cf command 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.

  4. Now that the migrations have run you can deploy your application:
    $ cf push <appname> -c "null"

    The cf command-line tool stores the last command passed to -c and uses it every time we deploy our application. By passing "null" to -c we can clear the previous command and use the buildpack's default start script to run our application server.

  5. Congratulations, you now have a Ruby on Rails application running on Bluemix!Status of pushing Rails application to Bluemix' cloud
  6. View the application in your browser by clicking your app's link on Bluemix.Sample Rails blog app

    Click to see larger image

    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.

  7. 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 Work with Tables. In Schemas, choose the one that starts with the letter U ( U71290, for example), which is the user name of the service. In Table Name, click POSTS, then click Browse 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.

BLUEMIX SERVICE USED IN THIS TUTORIAL:The SQL Database service adds an on-demand relational database to your application.

Add a comment

Note: HTML elements are not supported within comments.

1000 characters left

developerWorks: Sign in

Required fields are indicated with an asterisk (*).

Need an IBM ID?
Forgot your IBM ID?

Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.


All information submitted is secure.

Zone=Cloud computing
ArticleTitle=Build a blog app with Ruby on Rails and Bluemix