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

  1. Install the Rails gem:

    $ gem install rails

    Example output

    Successfully installed rails-4.3.0
    1 gem installed

  2. After you install the Rails gem, create a new application with PostgreSQL as your database:

    $ rails new rails-app --database=postgresql
  3. Change into your new application directory:

    $ cd rails-app
  4. If you already have an application, make sure the pg (postgresql) gem is present in your Gemfile. If not, edit your Gemfile by adding the gem:

    gem 'pg'
  5. Generate a new Gemfile.lock with all your dependencies:

    $ bundle install
  6. In addition to using the postgresql database with the pg gem, you also must ensure that the config/database.yml is using the postgresql adapter.

    Make sure you updated default section in the config/database.yml file, so it looks like this:

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      host: localhost
      username: rails
      password:
  7. Create your application’s development and test databases:

    $ rake db:create

    This creates development and test 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

  1. Run Rails generator:

    $ rails generate controller welcome index

    All the necessary files are created.

  2. edit line 2 in config/routes.rb file as follows:

    root 'welcome#index'
  3. 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 your config/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

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

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

  3. Set the remote that points to your git repository:

    $ git remote add origin git@github.com:<namespace/repository-name>.git
  4. Push your application to your remote git repository.

    $ git push
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.