How to Host Ruby on Rails 4 Apps on OpenShift

Ruby 2.0 is Here

With the latest release to OpenShift Online, we’ve added Ruby 2.0 (Ruby 2.0.0p353 to be exact) and with that comes a supported Rails 4 quickstart.

ruby2-and-rails4-native-on-openshift-online

If you need to get rails 4 working on ruby 1.9.3 see below..

Ruby 1.9.3 and Rails 4

In this article, I’ll be showing you how to get Rails 4 running on OpenShift Online with MongoDB as its database. Rails and MongoDB can make a pretty incredible combination and when configured correctly, can run a fast, reliable, and robust web application. Lets get started.

Initial Setup to Host Ruby on Rails 4 Apps on OpenShift

Before we do anything with Rails, we need to first setup our application on OpenShift. We can accomplish this in one easy to run command using the rhc tool.
rhc app create ruby-1.9 -a rails4

rhc app create screenshot

Once that’s finished, next we’ll need to add the MongoDB cartridge to our application.
rhc cartridge add mongodb-2.4 -a rails4

rhc cart add Mongo

Prepping for Rails 4

You may have noticed that when we did the rhc app create, it already cloned the app to our local machine. So now, lets cd into that directory and get to work.

new app screenshot

First thing we need to do is clean up what was automatically created for us:

rm -rf * (be careful with this command, make sure you’re in the right directory or you might delete more than you wanted)

Then, just to be thorough, we’ll commit everything so we can easily recover if things go wrong.

git add --all
git commit -am "Cleaning up everything"

Cleaning up app

Creating our Rails site:

Before we start creating our rails site, I need explain a few things. Currently, OpenShift Online only supports ruby-1.9.3 and 2.0 is on its way and will be here soon. In the meantime you will need be able to manage the two versions on your local machine. Thankfully, there is rvm and this tool makes using different Ruby versions a breeze.

Next, we’ll need to tell rvm to use ruby-1.9.3 for this particular project. If you haven’t installed rvm yet, then I would suggest doing so now. Without rvm, managing your ruby versions can be rather difficult.

rvm use ruby-1.9.3

rvm use 1.9.3

Now that we have ruby setup properly, lets make sure we update to the latest version of Rails as well. For this tutorial I’ll be using V. 4.0.3

gem update rails

Now that all the boring stuff is out of the way, lets create our Rails site.

rails new ./ --skip-active-record

New rails site - skip activerecord implementation

The --skip-active-record will do exactly what it says, which is skip activerecord implementation when generating our site. Since we’re using MongoDB, we’ll need to use activemodel instead. This step is crucial for getting MongoDB to work with Rails.

Next, we’ll need to add a few things to our Gemfile.

gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'  
gem 'twitter-bootstrap-rails'  
gem 'less-rails'  
 
be sure to uncomment: 
gem 'therubyracer', platforms: :ruby

Don’t worry about the gem’s we added right now, I’ll explain them as we go on.

Ultimately your Gemfile should look something like this:

Gemfile screenshot

Once you’re finished with that, just bundle everything up and we’ll move onto the next step.

bundle install

bundle install screenshot

Configure Rails for MongoDB

In this section I’ll show you how to setup Mongoid to work with Rails and OpenShift.

First we’ll need to generate our database yaml file.

rails g mongoid:config

Mongoid Generate

Then we need to open up ./config/mongoid.yml and add in our OpenShift Database parameters. I’ve gone ahead and typed everything below, so all you need to do is copy and paste it to the bottom of the file.

<% if ENV['OPENSHIFT_MONGODB_DB_HOST'] %>
production:
  sessions:
    default:
      database: <%= ENV['OPENSHIFT_APP_NAME'] %>
      hosts:
        - <%="#{ENV['OPENSHIFT_MONGODB_DB_HOST']}:#{ENV['OPENSHIFT_MONGODB_DB_PORT']}" %>
      username: <%= ENV['OPENSHIFT_MONGODB_DB_USERNAME'] %>
      password: <%= ENV['OPENSHIFT_MONGODB_DB_PASSWORD'] %>
<% end %>

Mongoid yaml

Create a MVC object and give it some style.

So far we’ve done quite a bit. We’ve created a ruby app, assigned it a DB, generated our Rails site, and configured it to use MongoDB. Now we’ll generate some Models, Views, and Controllers to give our application some functionality. Plus, we’ll give a little style so its a bit easier on the eyes.

Lets generate our first scaffold called Posts and give them a title and a body

rails g scaffold Posts Title:string Body:string

rails scaffold for Posts

Before we move on to the next step, we’ll need to make one small change to the Posts modelapp/models/post.rb and add field :created_at, type:Date. You could also append the scaffold command above to add “created_at:Date” but I didn’t catch this until after.

Date command

Once that’s finished, we can test things locally to see what they look like (make sure you have mongodb installed on your local system and its running).

rails -s and open your browser to 0.0.0.0:3000/posts

Preview

testing Posts locally screenshot

For the sake of this tutorial, let’s make the Posts index page the root of our site. To do so, all we need to do is add one line into our config/routes.rb file.

root 'posts#index'

Change webroot

Styling

Things look a little rough so lets add some style. Thankfully, this is pretty easy since we installed twitter-bootstrap-rails.

Before we start editing the Posts index page, we need to run the bootstrap generator to add bootstrap to the assets

rails g bootstrap:install less

bootstrap Install

Then we can use rails g bootstrap:themed Posts to quickly theme our posts.

theming posts

….. and Volia we should be all set.

Lets see what it looks like locally
rails s and open your browser to 0.0.0.0:3000/posts

Preview

styled posts example

Now our Posts have the general bootstrap theme applied. While it still doesn’t look fantastic, its a start! If you’re interested in a great open source bootstrap theme, check out https://www.patternfly.org/

Pushing things up to Openshift.

Now that we have a Rails 4 app that uses MongoDB configured for OpenShift, we can push things up. Simply add, commit, and push, then let OpenShift take care of the rest.

git add --all
git commit -am "site created and DB configured"
git push

Deploy app on openshift

Thats it – it’s out there! Just open up your browser and go to the url for your app!

Preview

Here’s a look at the first post

Lastly, I’ll be creating a Rails4 quickstart in the near future to make deploying your Rails4 site on OpenShift even easier! Stand by for an update.

Categories
MongoDB, OpenShift Online, Ruby
Tags
  • tarek

    hello, why created_at, Date generates issues. I even removed and refreshed the cache but it’s still shown in the index page? I don’t think the rake cache refresh works.

  • gosukiwi

    I wonder why it uses MongoDB instead of Posgres or MySQL 😛

  • Swistaczek

    Hey, please provide information how could I use mongoid and rails while auto scaling is enabled? I have in mind issue with ‘resolv’ which is used inside of Mongoid Address resolver.