This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.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
$ gem install rails
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Successfully installed rails-4.3.0 1 gem installed
Successfully installed rails-4.3.0 1 gem installed
Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you install the Rails gem, create a new application with PostgreSQL as your database:
rails new rails-app --database=postgresql
$ rails new rails-app --database=postgresql
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change into your new application directory:
cd rails-app
$ cd rails-app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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'
gem 'pg'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate a new
Gemfile.lock
with all your dependencies:bundle install
$ bundle install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create your application’s development and test databases:
rake db:create
$ rake db:create
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
$ rails generate controller welcome index
Copy to Clipboard Copied! Toggle word wrap Toggle overflow All the necessary files are created.
edit line 2 in
config/routes.rb
file as follows:root 'welcome#index'
root 'welcome#index'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the rails server to verify the page is available:
rails server
$ rails server
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.
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 fileCopy to Clipboard Copied! Toggle word wrap Toggle overflow
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
$ ls -1
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the following commands in your Rails app directory to initialize and commit your code to git:
git init
$ git init
Copy to Clipboard Copied! Toggle word wrap Toggle overflow git add .
$ git add .
Copy to Clipboard Copied! Toggle word wrap Toggle overflow git commit -m "initial commit"
$ git commit -m "initial commit"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
$ git remote add origin git@github.com:<namespace/repository-name>.git
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Push your application to your remote git repository.
git push
$ git push
Copy to Clipboard Copied! Toggle word wrap Toggle overflow