Ruby on Rails DEMO Ruby on Rails n
Ruby on Rails 概説
DEMO
Ruby on Railsの考え方 n Convention over Configuration – Co. C – 「設定よりも規約」 n Don't Repeat Yourself – DRY – 「同じことを繰り返さない」
MVCアーキテクチャ (Model-View-Controller)
MVCアーキテクチャ (Model-View-Controller) n Model n View n Controller
Railsの構成要素 Active. Support (Rubyクラスの拡張) n Action. Pack n – Action. View (View) – Action. Controller (Controller) Active. Record (DB接続, Model) n Active. Web. Service (Webサービス) n Action. Mailer (メールの送受信) n railties (周辺ライブラリ・ツール) n
1. Rubyのインストール n Windowsなら One-Click Ruby Installer を 利用すると楽 n http: //rubyforge. org/projects/rubyinstaller / n ruby 186 -25. exe をダウンロード n ダブルクリックしてインストール開始
生成された app/controllers/hello_controller. rb # コントローラ名は Hello + Controller # Application. Controllerを継承 class Hello. Controller < Application. Controller #inputアクション def input end # greetingアクション def greeting end
URLとコントローラ n http: //localhost: 3000/hello/input n http: //localhost: 3000/hello/greeting n hello がコントローラ名 n inputとgreetingがアクション名 # コントローラ名は Hello + Controller class Hello. Controller < Application. Controller #inputアクション def input end # greetingアクション def greeting end
app/views/hello/input. rhtmlに フォームを追加 <h 1>お名前を入力</h 1> ボタンを押したらgreeting アクションに移る <% form_tag : action => : greeting do %> <%= text_field : input, : name %> <%= submit_tag 'OK' %> <% end %> text_filed object_name, method, options = {}
app/views/hello/greeting. rhtml を編集 <%= @str %> greetingアクションの@str変数の値を 表示
greetingで名前を表示する n app/controllers/hello_controller. rb を編集 def greeting @name = @params[: input][: name] end n app/views/hello/greeting. rhtmlを編集 <h 1>ごあいさつ</h 1> <%= "こんにちは、#{@name}さん" %> <% form_tag : action => : input do %> <%= submit_tag '戻る' %> <% end %>
config/routes. rb Action. Controller: : Routing: : Routes. draw do |map| map. connect ': controller/service. wsdl', : action => 'wsdl' map. connect ': controller/: action/: id. : format' map. connect ': controller/: action/: id' end # # # map. connect ‘: controller/: action/: id’ に着目 URLの形式が <コントローラ名>/<アクション名>/<id> で あることを示している このファイルに map. connect 'h/: action/: id', : controller => 'hello' # を追加したらどのように動くだろうか?
基本的な作業手順 (本の管理アプリケーション) n n n n n データベース(My. SQL)の準備 rails books config/database. yml の編集 My. SQLでデータベース作成 ruby script/generate model book title: string author: string publisher: string isbn: string published_on: date rake db: migrate ruby script/generate scaffold Book サーバの立ち上げ アプリケーションの修正
database. yml development: adapter: mysql database: books_development username: root password: tomoharu socket: /var/lib/mysql. sock encoding: utf 8 test: database: books_test …… production: database: books_test ……
データソースのアーキテクチャ パターン n Table Data Gateway n Row Data Gateway n Active Record n Data Mapper
Active Record とCRUD n Create b = Book. new b. title = ‘…’; b. save n Read b = Book. find(id)
Active Record とCRUD n Update b = Book. find(id) b. title = ‘…’ b. save n Delete b = Book. find(id) b. destroy
findメソッド books = Book. find(: all, : conditions=>[“title like ? ”, % + Ruby + %] # conditions の値は、SQLのwhere句の内容
Ajax
Ajax ということば n “Ajax”という名前のはじまり – – n Ajax: A New Approach to Web Applications Jesse James Garrett 氏 2005年 2月18日 http: //www. adaptivepath. com/publications/essays/archives/00 0385. php 日本語訳 – Ajax: Web アプリケーション開発の新しいアプローチ – けんたろ 氏 – http: //antipop. gs/docs/translations/ajax. html
Ajax n Ajax = Asynchronous Java. Script + XML – Asynchronous = 非同期 n 技術的な特徴 – Ajax Engineを用いる – Webサーバと非同期に通信する
Ajaxの構成技術 n HTML (XHTML) n Java. Script n DOM (Document Object Model) n CSS (Cascading Style Sheet) n XMLHttp. Request
Java. Script: 世界で最も誤解され たプログラミング言語 n (原文) Java. Script: The World's Most Misunderstood Programming Language – http: //www. crockford. com/javascript/javascri pt. html n http: //d. hatena. ne. jp/brazil/20050829/112 5321936
Java. Scriptライブラリの利用 n n n n n DOJO Toolkit� Prototype. js Yahoo! UI Library script. aculo. us Rico Google Web Toolkit Zimbra Kabuki ……
バリデータ n 入力された値を検証するためのもの n モデルに記述 class Book < Active. Record: : Base validates_presence_of : title, : author validates_uniqueness_of : isbn validates_format_of : isbn, : with => /[0 -9 x. X¥-]+/ end
バリデータの種類 (1) #必須項目のチェック validates_presence_of : title, $author #長さチェック validates_length_of : username, : minimum => 6, : maximum => 8 #数値であるかチェック validates_numericality_of : number
準備 n config/environment. rb を編集 require 'gettext/rails’ n app/controllers/application. rb を編集 init_gettext 'book' # プロジェクト名を指定
Gettext Scaffold generator プラグイン n プラグイン – Railsの機能を拡張するためのしくみ n Gettext Scaffold generator – これを使うとpoファイルやmoファイルの作成が楽にな る n インストール ruby script/plugin install svn: //rubyforge. org/var/svn/gettextscaffold/ge ttext_scaffold
利用手順 (2) n po/ja/books. po ファイルの編集 msgid "Book|Author” msgstr "著者"
- Slides: 77