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 railsCopy 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 installedCopy 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=postgresqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change into your new application directory:
cd rails-app
$ cd rails-appCopy 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 yourGemfileby adding the gem:gem 'pg'
gem 'pg'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate a new
Gemfile.lockwith all your dependencies:bundle install
$ bundle installCopy to Clipboard Copied! Toggle word wrap Toggle overflow In 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create your application’s development and test databases:
rake db:create
$ rake db:createCopy to Clipboard Copied! Toggle word wrap Toggle overflow This 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 index
$ rails generate controller welcome indexCopy to Clipboard Copied! Toggle word wrap Toggle overflow All the necessary files are created.
edit line 2 in
config/routes.rbfile 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 serverCopy 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
defaultsection in yourconfig/database.ymlwith pre-defined variables as follows:Sample
config/databaseYAML 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 -1command. The output of the command should look like:ls -1
$ ls -1Copy 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 initCopy 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
gitrepository:git remote add origin git@github.com:<namespace/repository-name>.git
$ git remote add origin git@github.com:<namespace/repository-name>.gitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Push your application to your remote git repository.
git push
$ git pushCopy to Clipboard Copied! Toggle word wrap Toggle overflow