1 / 15

ビューとコントローラ

ビューとコントローラ. やりたいこと. http://localhost:3000/hello/input にアクセス 名前を入力すると http://localhost:3000/hello/greeting で挨拶してくれる データベースは利用しない. Rails のプロジェクト作成. 以下のコマンドを入力して、 hello というプロジェクトを作成する andoh$ rails hello. コントローラの作成. hello ディレクトリに移動して、次のコマンドを実行

aviva
Télécharger la présentation

ビューとコントローラ

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. ビューとコントローラ

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

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

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

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

  6. 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 ビューとコントローラ

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

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

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

  10. 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 = {} ビューとコントローラ

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

  12. app/views/hello/greeting.rhtmlを編集 <%= @str %> greetingアクションの@str変数の値を 表示 ビューとコントローラ

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

  14. 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 %> ビューとコントローラ

  15. 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' # を追加したらどのように動くだろうか? ビューとコントローラ

More Related