11.3. Writing your application
If you are starting your Rails application from scratch, you must install the Rails gem first. Then you can proceed with writing your application.
Procedure
Install the Rails gem:
$ gem install rails
Example output
Successfully installed rails-4.3.0 1 gem installed
After you install the Rails gem, create a new application with PostgreSQL as your database:
$ rails new rails-app --database=postgresql
Change into your new application directory:
$ cd rails-app
If you already have an application, make sure the
pg
(postgresql) gem is present in yourGemfile
. If not, edit yourGemfile
by adding the gem:gem 'pg'
Generate a new
Gemfile.lock
with all your dependencies:$ bundle install
In addition to using the
postgresql
database with thepg
gem, you also must ensure that theconfig/database.yml
is using thepostgresql
adapter.Make sure you updated
default
section in theconfig/database.yml
file, so it looks like this:default: &default adapter: postgresql encoding: unicode pool: 5 host: localhost username: rails password:
Create your application’s development and test databases:
$ rake db:create
This creates
development
andtest
database in your PostgreSQL server.
11.3.1. Creating a welcome page
Since Rails 4 no longer serves a static public/index.html
page in production, you must create a new root page.
In order to have a custom welcome page must do following steps:
- Create a controller with an index action.
- Create a view page for the welcome controller index action.
- Create a route that serves applications root page with the created controller and view.
Rails offers a generator that completes all necessary steps for you.
Procedure
Run Rails generator:
$ rails generate controller welcome index
All the necessary files are created.
edit line 2 in
config/routes.rb
file as follows:root 'welcome#index'
Run the rails server to verify the page is available:
$ rails server
You should see your page by visiting http://localhost:3000 in your browser. If you do not see the page, check the logs that are output to your server to debug.
11.3.2. Configuring application for OpenShift Container Platform
To have your application communicate with the PostgreSQL database service running in OpenShift Container Platform you must edit the default
section in your config/database.yml
to use environment variables, which you must define later, upon the database service creation.
Procedure
Edit the
default
section in yourconfig/database.yml
with pre-defined variables as follows:Sample
config/database
YAML file<% user = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? "root" : ENV["POSTGRESQL_USER"] %> <% password = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? ENV["POSTGRESQL_ADMIN_PASSWORD"] : ENV["POSTGRESQL_PASSWORD"] %> <% db_service = ENV.fetch("DATABASE_SERVICE_NAME","").upcase %> default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV["POSTGRESQL_MAX_CONNECTIONS"] || 5 %> username: <%= user %> password: <%= password %> host: <%= ENV["#{db_service}_SERVICE_HOST"] %> port: <%= ENV["#{db_service}_SERVICE_PORT"] %> database: <%= ENV["POSTGRESQL_DATABASE"] %>
11.3.3. Storing your application in Git
Building an application in OpenShift Container Platform usually requires that the source code be stored in a git repository, so you must install git
if you do not already have it.
Prerequisites
- Install git.
Procedure
Make sure you are in your Rails application directory by running the
ls -1
command. The output of the command should look like:$ ls -1
Example output
app bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile README.rdoc test tmp vendor
Run the following commands in your Rails app directory to initialize and commit your code to git:
$ git init
$ git add .
$ git commit -m "initial commit"
After your application is committed you must push it to a remote repository. GitHub account, in which you create a new repository.
Set the remote that points to your
git
repository:$ git remote add origin git@github.com:<namespace/repository-name>.git
Push your application to your remote git repository.
$ git push