Hello World from OSS Silicon Valley


HowToUse/RubyOnRails/4.2/PostgreSQL/9.3

_ Prerequisite

_ Install&Setup

Step.1
Check the version of installed postgresql.
$ su - postgres
$ psql --version
Step.2
Install module.
$ sudo apt-get install postgresql-server-dev-9.3

_ HowToUse

Step.1
Create rails project.
$ rails new <project name> --database=postgresql

(Example)

$ rails new sample --database=postgresql
Step.2
Edit Gemfile.
$ vi Gemfile

gem 'therubyracer', platforms: :ruby
...
gem 'execjs'

(You can see sample from here)

Step.3
Install gems.
$ bundle install
Step.4
Update pg_hba.conf from "peer" to "trust" so that other user than postgres can access database as exepected.
$ su - postgres
$ cd /etc/postgres/9.3/main
$ vi pg_hba.conf
local   all             postgres                                trust
local   all             all                                     trust
Step.5
Restart postgres daemon.
$ /etc/init.d/postgres restart
Step.6
Create DB user for application.
$ su - postgres
$ psql -d postgres
postgres=# create role <user_name> login createdb;
postgres=# \q

(Example)

postgres=# create role sample login createdb;
Step.7
Edit database configuration for rails application. Put the same user name as you created in postgres database.
$ vi config/database.yml
default:
  ...
  username: <user_name>

(Example)

  username: sample

(You can see sample from here)

Step.8
Create new controller for welcome page.

(Example)

$ rails generate controller welcome
Step.9
Create welcome page.

(Example)

$ vi app/views/welcome/index.html.erb 
<h2>Hello World</h2>
<p>
<a href="/users">user list</a>
<br>
<%= Time.now %>
</p>

(You can see sample from here)

Step.11
Create User list.

(Example)

$ rails generate scaffold user
Step.10
Config routing setting
$ vi config/routes.rb
root 'welcome#index'

Create and migrate initial database.

$ rake db:create
$ rake db:migrate
Step.9
Check if you can connect database from your rails application's user.
$ rails dbconsole
Step.10
Launch server instance.
$ rails s

You will see the following window.

CreateApps_fig1.png

And also, you can edit user list.

CreateApps_fig2.png

_ Author

S.Yatsuzuka

 
Attach file: fileCreateApps_fig2.png 202 download [Information] fileCreateApps_fig1.png 198 download [Information]
Last-modified: 2016-02-28 (Sun) 03:40:46 (2978d)