1 / 77

Ruby on Rails 入門

Ruby on Rails 入門. 2007-08-08 稚内北星学園大学 安藤 友晴. Ruby on Rails 概説. Ruby on Rails とは何か. MVC アーキテクチャ による Web アプリケーションフレームワーク プログラミング言語として Ruby を利用 名前でわかるけど フルスタック Web 周りから DB 周りまで. 諸元. 作者 David Heinemeier Hansson 略して “ DHH ” と呼ばれることが多い ライセンス MIT ライセンス 歴史 2004 年 7 月 公開

loring
Télécharger la présentation

Ruby on Rails 入門

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby on Rails入門 2007-08-08 稚内北星学園大学 安藤 友晴

  2. Ruby on Rails 概説

  3. Ruby on Railsとは何か • MVCアーキテクチャによるWebアプリケーションフレームワーク • プログラミング言語としてRubyを利用 • 名前でわかるけど • フルスタック • Web周りからDB周りまで

  4. 諸元 • 作者 • David Heinemeier Hansson • 略して “DHH”と呼ばれることが多い • ライセンス • MITライセンス • 歴史 • 2004年7月 公開 • 2005年12月13日 1.0 • 2007年3月14日 1.2

  5. DEMO

  6. Ruby on Railsの考え方 • Convention over Configuration • CoC • 「設定よりも規約」 • Don't Repeat Yourself • DRY • 「同じことを繰り返さない」

  7. MVCアーキテクチャ(Model-View-Controller)

  8. MVCアーキテクチャ(Model-View-Controller) • Model • View • Controller

  9. Railsの構成要素 • ActiveSupport (Rubyクラスの拡張) • ActionPack • ActionView (View) • ActionController (Controller) • ActiveRecord (DB接続, Model) • ActiveWebService (Webサービス) • ActionMailer (メールの送受信) • railties (周辺ライブラリ・ツール)

  10. 環境構築 (Windows版)

  11. 1. Rubyのインストール • Windowsなら One-Click Ruby Installer を利用すると楽 • http://rubyforge.org/projects/rubyinstaller/ • ruby186-25.exe をダウンロード • ダブルクリックしてインストール開始

  12. 2. RubyGemsのインストール • RubyGemsはRubyのパッケージ管理システム • One-Click Ruby Installer を使っていれば既にインストールされている • そうでない人は下記サイトからダウンロード • http://rubyforge.org/projects/rubygems/

  13. 3. PATHを通す • <Rubyのインストール先>/bin ディレクトリにPATHを通しておく • RubyGemsを別途インストールしている場合にはそこにもPATHを通す • ruby -v コマンドの実行結果が次のようになればOK $ ruby -v ruby 1.8.6 (…) $

  14. 4. Railsのインストール • 次のコマンドでRailsをインストール • gem install rails --include-dependencies • Rails(と関連するソフトウェア)がインストールされる

  15. 5. MySQLのインストール • rootユーザの再設定が必要 • 文字コードはUTF-8で • 詳細は別紙プリントを参照

  16. Ruby超特急

  17. Rubyとは • 「まつもと ゆきひろ」氏が作成したプログラミング言語 • 1995年に発表 • オープンソースソフトウェア • 「オブジェクト指向スクリプト言語」 • Rubyを使った「Ruby on Rails」によって、最近一躍有名に。 • http://www.ruby-lang.org/

  18. ビューとコントローラ

  19. やりたいこと • http://localhost:3000/hello/inputにアクセス • 名前を入力すると • http://localhost:3000/hello/greetingで挨拶してくれる • データベースは利用しない

  20. Railsのプロジェクト作成 • 以下のコマンドを入力して、helloというプロジェクトを作成する • andoh$ rails hello

  21. コントローラの作成 • hello ディレクトリに移動して、次のコマンドを実行 • andoh$ ruby script/generate controller hello input greeting • hello というコントローラと、input と greeting というアクションを作成する。

  22. 生成されたapp/controllers/hello_controller.rb # コントローラ名は Hello + Controller # ApplicationControllerを継承 class HelloController < ApplicationController #inputアクション def input end # greetingアクション def greeting end end

  23. URLとコントローラ • http://localhost:3000/hello/input • http://localhost:3000/hello/greeting • hello がコントローラ名 • inputとgreetingがアクション名 # コントローラ名は Hello + Controller class HelloController < ApplicationController #inputアクション def input end # greetingアクション def greeting end end

  24. Railsアプリケーションの動作 • DispatcherがRequestを受け取る (HTTP) • コントローラとアクションを決定 • コントローラ中のアクションが処理 • ビューを処理 • ブラウザにResponseを返す (HTTP)

  25. Railsのビュー • eRubyフォーマットの実装であるERBが担当 • eRubyはRubyのテンプレートエンジン • “(アクション名).rhtml” というファイル名

  26. サーバを起動する • ruby script/server コマンドを実行 • WEBrick という Rails組み込みのWebサーバが起動される • デフォルトでは3000ポートで起動 • アクションに対応したビューが表示される • http://localhost:3000/hello/input • http://localhost:3000/hello/greeting

  27. app/views/hello/input.rhtmlにフォームを追加 <h1>お名前を入力</h1> <% form_tag :action => :greeting do %> <%= text_field :input, :name %> <%= submit_tag 'OK' %> <% end %> ボタンを押したらgreetingアクションに移る text_filed object_name, method, options = {}

  28. app/controllers/hello_controller.rbを編集 # @params はパラメータ情報を管理するため # のインスタンス変数 # アクションで定義されたインスタンス変数は # ビューで利用できる def greeting @str = @params.inspect end

  29. app/views/hello/greeting.rhtmlを編集 <%= @str %> greetingアクションの@str変数の値を 表示

  30. 実行 • input のテキストフィールドに「安藤友晴」と入力してボタンを押すと • greeting で次の文字列が表示される { "commit"=>"OK", "action"=>"greeting", "controller"=>"hello", "input"=>{"name"=>"安藤友晴"} } • これがパラメータの情報になる

  31. greetingで名前を表示する • app/controllers/hello_controller.rb を編集 def greeting @name = @params[:input][:name] end • app/views/hello/greeting.rhtmlを編集 <h1>ごあいさつ</h1> <%= "こんにちは、#{@name}さん" %> <% form_tag :action => :input do %> <%= submit_tag '戻る' %> <% end %>

  32. config/routes.rb ActionController::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' # を追加したらどのように動くだろうか?

  33. データベースの取り扱い

  34. 基本的な作業手順(本の管理アプリケーション)基本的な作業手順(本の管理アプリケーション) • データベース(MySQL)の準備 • rails books • config/database.yml の編集 • MySQLでデータベース作成 • ruby script/generate model book title:string author:string publisher:string isbn:string published_on:date • rake db:migrate • ruby script/generate scaffold Book • サーバの立ち上げ • アプリケーションの修正

  35. database.yml development: adapter: mysql database: books_development username: root password: tomoharu socket: /var/lib/mysql/mysql.sock encoding: utf8 test: database: books_test …… production: database: books_test ……

  36. エンタープライズアプリケーションアーキテクチャパターンエンタープライズアプリケーションアーキテクチャパターン • マーチン・ファウラー著 • 翔泳社 • ISBN: 4-7981-0553-8 • エンタープライズ・アプリケーションの設計技法について。オブジェクト指向設計全般で参考になる • 原書名: 「Patterns of Enterprise Application Architecture」 • 翻訳の質は微妙 • 「PofEAA」とか「PoEAA」と呼ばれる • http://capsctrl.que.jp/kdmsnr/wiki/PofEAA/

  37. データソースのアーキテクチャパターン • Table Data Gateway • Row Data Gateway • Active Record • Data Mapper

  38. Active Record パターン • データと振る舞いの両方を持つオブジェクト。Personクラス自身にデータベースにアクセスする処理が記述されている。

  39. データベースに対するCRUD • Create • テーブルへの情報の追加 • insert into 文 • Read • テーブルからの情報の読み出し • select文 • Update • テーブルの情報の更新 • update文 • Delete • テーブルの情報の削除 • delete文

  40. Active Record とCRUD • Create b = Book.new b.title = ‘…’; b.save • Read b = Book.find(id)

  41. Active Record とCRUD • Update b = Book.find(id) b.title = ‘…’ b.save • Delete b = Book.find(id) b.destroy

  42. findメソッド books = Book.find(:all, :conditions=>[“title like ?”, % + Ruby + %] # conditions の値は、SQLのwhere句の内容

  43. Ajax

  44. Ajax ということば • “Ajax”という名前のはじまり • Ajax: A New Approach to Web Applications • Jesse James Garrett 氏 • 2005年2月18日 • http://www.adaptivepath.com/publications/essays/archives/000385.php • 日本語訳 • Ajax: Web アプリケーション開発の新しいアプローチ • けんたろ 氏 • http://antipop.gs/docs/translations/ajax.html

  45. Ajax • Ajax = Asynchronous JavaScript + XML • Asynchronous = 非同期 • 技術的な特徴 • Ajax Engineを用いる • Webサーバと非同期に通信する

  46. Ajax Engineを用いる • 従来のクライアントとサーバの間に Ajax Engine が入る • Ajax Engine • ユーザインタフェースのレンダリング • 必要に応じてサーバと通信する

  47. Webサーバと非同期に通信する • WebブラウザとWebサーバが通信しているあいだ、クライアント側では待ち時間が発生しない

  48. Ajaxの構成技術

  49. Ajaxの構成技術 • HTML (XHTML) • JavaScript • DOM (Document Object Model) • CSS (Cascading Style Sheet) • XML • XMLHttpRequest

  50. JavaScript • (基本的には)Webブラウザ上で動作するプログラミング言語 • Java言語とは関係ない • オブジェクト指向言語 • 「関数型言語」でもある • “ECMAScript”として標準化されている

More Related