- 追加された行はこの色です。
- 削除された行はこの色です。
* Ruby on Rails [#wd09c854]
#setlinebreak(on);
#contents()
-- 関連
--- [[Ruby]]
--- [[Rubyのインストール]]
--- [[Rails のインストール]]
--- [[Railsメモ]]
--- [[ApacheとRailsをmod_proxyで連携する]]
--- [[Railsをthinで複数起動してApacheでロードバランス]]
--- [[websocket-railsインストール]]
** Ruby on Rails とは [#z16e3df3]
#html(<div style="padding-left:20px;">)
スクリプト言語の[[Ruby]]により構築された、RESTfulなWebアプリケーション開発のためのフレームワーク。
MVC(Model-View-Controller)アーキテクチャをサポートしている。
特徴的なのは、モデル、ビュー、コントローラの雛形を自動生成する機能。
簡単なマスタメンテ画面であればテーブル作成するだけで、処理を自動生成できる。
但し、自動生成された画面に対しては、あんまり細かい事を言わないのが前提。
#html(</div>)
** railsプロジェクトの作成 [#oac4cb99]
#html(<div style="background:#000000;color:white;padding:5px 10px;margin-left:20px;">)
rails new -d mysql rails_example
#html(</div>)
※上記の場合、カレントディレクトリに rails_example ディレクトリが作成される
** データベースとユーザの作成 [#b197cea7]
#html(<div style="background:#000000;color:white;padding:5px 10px;margin-left:20px;">)
mysql -u root -p
Enter password:
mysql> create database rails_example character set utf8;
Query OK, 1 row affected (0.01 sec)
mysql> create user rails_example@localhost identified by 'rails_example';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON rails_example.* TO 'rails_example'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
#html(</div>)
** 鍵の生成 [#w5aa2b27]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
bundle install
bundle exec rake secret
#html(</div>)
上記で表示された鍵を環境変数に設定しておく
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
export SECRET_KEY_BASE=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#html(</div>)
#html(</div>)
以降は、実行前にプロジェクトのディレクトリに移動しておく
** モデルの生成 [#e7060bc6]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rails generate model xxxxx
#html(</div>)
#html(</div>)
** コントローラの作成 [#t137be1e]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rails generate controller xxxxx
#html(</div>)
#html(</div>)
** モデル、コントローラの生成 [#faf4d5b3]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rails generate scaffold モデル名 列名1:データ型1 列名2:データ型2 ..
#html(</div>)
*** scaffoldの例 [#h346fa97]
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rails generate scaffold book isbn:string title:string price:integer
#html(</div>)
*** scaffoldの例(migrationをスキップする場合) [#r6176b66]
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rails generate scaffold book isbn:string title:string price:integer --skip-migration
#html(</div>)
#html(<div style="margin-left:10px;"><p style="margin-left:10px;padding:0px;">尚、scaffoldで指定できるデータ型は以下の通り。)
|シンボル|説明|対応するMySQLの型|h
|:string|文字列|varchar(255)|
|:text|長い文字列|text|
|:integer|整数|int(11)|
|:float|浮動小数|float|
|:decimal|厳密な精度の少数|decimal|
|:datetime|日時|datetime|
|:timestamp|より細かい日時|datetime|
|:time|時間|time|
|:date|日付|date|
|:binary|バイナリデータ|blob|
|:boolean|真偽型|tinyint(1)|
#html(</div>)
#html(</div>)
** DBのmigrate [#vd22aac4]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rake db:migrate
#html(</div>)
#html(</div>)
** DB(Mysql)への日本語データ登録でエラーになる場合 [#y04411fb]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
# データベースの文字コード変更
alter database DB名 character set utf8;
# テーブルの文字コード変更
alter table テーブル名 convert to character set utf8;
#html(</div>)
#html(</div>)
** アクションの追加 [#x918ab8f]
#html(<div style="margin-left:20px;">)
※参照:http://d.hatena.ne.jp/zariganitosh/20080203/1202091772
(1) config/routes.rb に下記を追加
ActionController::Routing::Routes.draw do |map|
.
.
.
map.resources :employees, :collection => { :action1 => :get }
.
.
end
※employees はコントローラ名
(2) employees_controller.rb にメソッドを追加
#html(</div>)
** 定義済みアクションの確認 [#p6e2a87d]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rake routes
#html(</div>)
#html(</div>)
** rakeのタスク一覧の確認 [#p385e507]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rake -T
#html(</div>)
#html(</div>)
** サーバの起動 [#x313ab83]
#html(<div style="margin-left:20px;">)
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rails server -e production
#html(</div>)
#html(</div>)
** ActiveRecordの使い方(途中) [#d9a5c416]
#html(<div style="margin-left:20px;">)
#mycode(){{
@employees = Employee.find(:all, :conditions => [ "name like ?", user_name])
}}
#html(</div>)
//**生成されるHTMLテンプレートのカスタマイズ
//
//以下のコマンドでHTMLテンプレートを lib 配下に作成し、変更する事でテンプレートのカスタマイズが可能
//#html(<div style="background:#000000;color:white;padding:5px 10px;">)
//rake rails:templates:copy
//#html(</div>)
** テーブルデータの初期化(FIXTUREを利用) [#o35f40f9]
#html(<div style="margin-left:20px;">)
*** test/fixtures/テーブル名.yml にテストデータを用意 [#j4264c53]
#mycode(){{
one:
name: EMP01
dept_id: 1
two:
name: EMP02
dept_id: 1
data3:
name: EMP03
dept_id: 1
data4:
name: EMP04
dept_id: 1
data5:
name: EMP05
dept_id: 1
}}
以下のようにスクリプトにする事も可能
#mycode(){{
<% 0.upto(9) do |n| %>
data<%= n %>:
name: EMP<%= sprintf("%02d", n) %>
dept_id: 1
<% end %>
}}
*** データのロード [#xbbf2282]
#html(<div style="background:#000000;color:white;padding:5px 10px;">)
rake db:fixtures:load FIXTURES=employees
#html(</div>)
#html(</div>)