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 railsExample output
Successfully installed rails-4.3.0 1 gem installedAfter you install the Rails gem, create a new application with PostgreSQL as your database:
$ rails new rails-app --database=postgresqlChange into your new application directory:
$ cd rails-appIf you already have an application, make sure the
pg(postgresql) gem is present in yourGemfile. If not, edit yourGemfileby adding the gem:gem 'pg'Generate a new
Gemfile.lockwith all your dependencies:$ bundle installIn addition to using the
postgresqldatabase with thepggem, you also must ensure that theconfig/database.ymlis using thepostgresqladapter.Make sure you updated
defaultsection in theconfig/database.ymlfile, 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:createThis creates
developmentandtestdatabase 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 indexAll the necessary files are created.
edit line 2 in
config/routes.rbfile as follows:root 'welcome#index'Run the rails server to verify the page is available:
$ rails serverYou 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.
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
defaultsection in yourconfig/database.ymlwith pre-defined variables as follows:Sample
config/databaseYAML 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 -1command. The output of the command should look like:$ ls -1Example output
app bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile README.rdoc test tmp vendorRun 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
gitrepository:$ git remote add origin git@github.com:<namespace/repository-name>.gitPush your application to your remote git repository.
$ git push