- The added line is THIS COLOR.
- The deleted line is THIS COLOR.
#contents
*Prerequisite [#q4efe545]
-Ubuntu Server (You can refer [[Overall/UbuntuServer]])
-RVM (You can refer [[Overall/RVM]])
*Install&Setup [#y05d363e]
:Step.1|
Check if the rails has already been installed.
$ rails --version
:Step.2|
If the rails has not been installed yet, execute the following command
$ gem install rails
*HowToUse [#p499ff68]
**Create new application [#n11fa587]
:Step.1|
Create new application.
$ rails new <Application Name>
:Step.2|
Launch application.
$ cd <Application Name>
$ rails server
In case you encounter the error message of "Could not find a JavaScript runtime.", execute the following commands.
$ gem install execjs
$ gem install therubyracer
Step.2|
Open the Gemfile and append the following lines.
$ vi Gemfile
gem ‘execjs’
gem ‘therubyracer’
$ bundle install
:Step.3|
Launch application.
$ cd <Application Name>
$ rails server
:Step.4|
Access the following page.
http://localhost:3000/
#ref(CreateApplication_fig1.png,,500x266,)
:Step.4|
When you are setting portforwarding with rails4.2, localhost doesn't work by default. If you want to do port-forwarding, put the following option.
$ rails start -b 0.0.0.0
http://stackoverflow.com/questions/27799260/rails-4-2-server-port-forwarding-on-vagrant-does-not-work
**Create sample program with Scaffold [#y098077d]
:Step.1|
Create objects withs scaffold.
$ rails generate scaffold <Object Name>
(Example)
$ rails generate scaffold TestObject name:string
:Step.2|
Prepare database.
$ rake db:migrate
:Step.3|
Launch the server and access http://<IP address>:3000/<Object Name>
#ref(CreateSample_fig1.png,,500x266,)
Now you have a page which maintains data stored in Sqlite DB.
**Create Say Hello program [#b5233530]
:Step.1|
Create controller and view files.
$ rails generate controller <Controller Name> <Method(View)> <Method(View)>
(Example)
$ rails generate controller Say hello goodbye
:Step.5|
:Step.2|
Access the following page.
http://localhost:3000/say/hello/
#ref(CreateApplication_fig2.png,,500x266,)
**Upload to Heroku [#hf6c5c2d]
:Step.1|Create new account in Heroku (https://id.heroku.com/login)
**Get data from RESTful service [#t20ffe9e]
:Step.1|
Edit controller.
#ref(Heroku_fig1.png,,500x266,)
$ vi app/controller/<Controller file>
:Step.2|Install Ruby 2.2.2
require 'open-uri'
require 'json'
res = open('<URL>')
code, message = res.status
if code == '200'
@result = JSON.parse(res.read)
end
$ rvm install ruby-2.2.2
:Step.2|
Put it to view file.
:Step.3|Install Heroku Toolbelt.
$ vi app/view/<View file>
$ wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh
<table>
<% @result.each do |data| %>
<tr>
<td><%= data['<Key#1>'] %></td>
<td><%= data['<Key#2>'] %></td>
</tr>
<% end %>
</table>
When you encounter error, you can use standalone edition with the following commands.
$ wget -qO- https://toolbelt.heroku.com/install.sh | bash
$ echo 'PATH="/usr/local/heroku/bin:$PATH"' >> ~/.profile
**Change table structures [#vc6eb3b5]
:Step.1|
Create migration script
:Step.4|Login to Heroku
$ rails g migration <Migration Script Name>
$ heroku login
(Example)
$ rails g migration AddColumnUsers
:Step.5| Prepare Git repository.
:Step.2|
Edit migration script.
$ git clone <Git Repository>
$ vi db/migrate/<Migration Script File Name>
or
(Example - change column)
$ rails g migration db/migrate/20150927000024_add_column_workorders.rb
$ cd <Ruby On Rails Project>
$ git init
$ git add .
$ git commit -m "initial commit"
class AddColumnWorkorders < ActiveRecord::Migration
def change
add_column :<table name>, :<column name>, :<data type>
remove_column :<table name>, :<column name>
end
end
:Step.6| Edit Gemfile because Heroku doesn't allow sqlite3, but PostgreSQL
$ vi config/Gemfile
(Example - add foreign key)
+gem 'pg'
-gem 'sqlite3'
+gem 'sqlite3', group: %w(test development), require: false
+gem 'pg', group: %w(production), require: false
class AddForeignKeyStatements < ActiveRecord::Migration
def change
add_foreign_key :<referring table name>, :<referred table name>
end
end
:Step.7|Install gem file.
If you set "statements" as referring table, and "calendars" as refered table, then your calendar_id in statements table will be set for foreign_key.
$ bundle install
:Step.8|Edit database.yml
(Example - remove foreign key)
$ vi db/database.yml
class RemoveForeignKeyStatements < ActiveRecord::Migration
def change
remove_foreign_key :<referring table name>, :<referred table name>
end
end
production:
<<: *default
- database: db/production.sqlite3
+ adapter: pg
+ database: db/production.pg
:Step.9|Create application in Heroku
$ heroku create <application name>
:Step.3|
Setup database
:Step.10|Push files.
$ rake db:create
$ rake db:migrate
$ git push heroku master
:Step.4|
Edit index.html.erb
$ vi app/views/<table>/index.html.erb
:Step.5|
Edit _form.html.erb
$ vi app/views/<table>/_form.html.erb
<%= f.label :<column> %>
<%= f.text_field :<column> %>
:Step.6|
Edit conroller. After Rails is version up to 4.x, we can't use "attr_accessible", so we need to define as below instead.
$ vi app/controller/<table name>_controller.rb
def create
attr = params.require(:<table name>).permit(:<column name>)
@<table name> = <table name>.new(attr)
**DB Migration [#m6866a5d]
(Initilization)
$ rake db:migrate
(Rollback)
$ rake db:rollback
(DB Dump)
$ rake db:schema:dump
(Drop Database)
$ rake db:drop
(Load Dump file)
$ rake db:setup
(Reset DB)
$ rake db:reset
This is equivalent to "rake db:drop" with "rake db:setup".
**Testing [#l2ff40b5]
:Step.1|
Prepare test script.
default script)
require 'test_helper'
class <Class Name>ControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
:Step.2|
Prepare test environment.
$ rake db:test:prepare
:Step.3|
Execute test script.
$ rake test
*Author [#la69134c]
S.Yatsuzuka