*ブログに戻る →ここ です。
参考サイトは下記にリンクを張っています。

Rubyの学習 free221
コントローラーで変数に配列の値を名札つけてテンプレートに出力する機能を試した。
オブジェクト「render」を使用し、JSON形式 や XML形式 で出力することも試した。

↓Rubyのプログラムを実行すると以下のように表示される。

何も加筆していない状態
ブラウザからアクセスする
URL://localhost:3000/yoshi_movies/index

YoshiMovies#index

Find me in app/views/yoshi_movies/index.html.erb ---------------------------------------------------- ウェブブラウザからアクセスする URL://localhost:3000/yoshi_movies/index {"name":"DrawingMother","old":2,"Location":"福山市","Zip":72901} 注、画面左上に上記の文字がひっそり出力される ソースコード 1|{"name":"DrawingMother","old":2,"Location":"福山市","Zip":72901} | | ----------------------------------------- ウェブブラウザからアクセスする URL://localhost:3000/yoshi_movies/index This XML file does not appear to have any style information associated with it. The document tree is shown below. <hash> <name>DrawingMother</name> <old type="integer">2</old> <Location>福山市</Location> <Zip type="integer">72901</Zip> </hash> 注、画面左上に雰囲気的にHTMLのようなタグが付いた形で出力される ソースコードを見てみる 1|<?xml version="1.0" encoding="UTF-8"?> 2|<hash> 3| <name>DrawingMother</name> 4| <old type="integer">2</old> 5| <Location>福山市</Location> 6| <Zip type="integer">72901</Zip> 7|</hash> 8| 9| ----------------------------------- ブラウザからアクセスする URL//localhost:3000/yoshi_movies/index

YoshiMovies#index

現在地:福山市 郵便番号:72901 本籍:Japan 名前:DrawingMother 年齢:1 Find me in app/views/yoshi_movies/index.html.erb 注、左上に文字が出力される ブラウザからソースコード見る <!DOCTYPE html> <html> <head> <title>Kittakasample</title> <link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/atsu_sada.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/atsu_sadachi.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/hiramemo.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/hiramemos.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/ishi_photos.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitem.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitem1.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitem2.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitem3.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitems.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitems1.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitems2.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/sadaitems3.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/yoshi_movies.css?body=1" media="all" rel="stylesheet" /> <script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/atsu_sada.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/atsu_sadachi.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/hiramemo.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/hiramemos.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/ishi_photos.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitem.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitem1.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitem2.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitem3.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitems.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitems1.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitems2.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/sadaitems3.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/yoshi_movies.js?body=1"></script> <script data-turbolinks-track="true" src="/assets/application.js?body=1"></script> <meta content="authenticity_token" name="csrf-param" /> <meta content="/dL13zr+RSTKxZ8Rij5gCQvYwKI4xm2TQnvbrsAKavg=" name="csrf-token" /> </head> <body> <h1>YoshiMovies#index</h1> <p> 現在地:福山市<br /> 郵便番号:72901<br /> 本籍:Japan<br /> 名前:DrawingMother<br /> <!--メモ、順番入れ変えてみた エラーが出た、変数を間違っていたので保存形式ではなかった、保存形式をUTF-8に変えた--> 年齢:1<br /> <!--保存形式をEUCへ変更したらエラーとなったのでUTF-8に戻した--> </p> <p>Find me in app/views/yoshi_movies/index.html.erb</p> </body> </html> ----------------------------------- ブラウザからアクセスする URL//localhost:3000/yoshi_movies/index.json {"name":"DrawingMother","old":1,"Location":"福山市","Zip":72901,"本籍":"Japan"} 注、左上にひっそり文字が出力される ブラウザからソースコード見る 1|{"name":"DrawingMother","old":1,"Location":"福山市","Zip":72901,"本籍":"Japan"} | | ----------------------------------- ブラウザからアクセスする URL//localhost:3000/yoshi_movies/index.xml This XML file does not appear to have any style information associated with it. The document tree is shown below. <hash> <name>DrawingMother</name> <old type="integer">1</old> <Location>福山市</Location> <Zip type="integer">72901</Zip> <本籍>Japan</本籍> </hash> ブラウザからソースコード見る 1|<?xml version="1.0" encoding="UTF-8"?> 2|<hash> 3| <name>DrawingMother</name> 4| <old type="integer">1</old> 5| <Location>福山市</Location> 6| <Zip type="integer">72901</Zip> 7| <本籍>Japan</本籍> 8|</hash> 9|

Saturday,November,30,2013

プログラムの表示

コントローラを使用して、配列の値を JSON形式 や XML形式 で出力する。 メソッド「respond_to」を使用して出力形式を選ぶ事ができる。 URL//localhost:3000/yoshi_movies/index URL//localhost:3000/yoshi_movies/index.json URL//localhost:3000/yoshi_movies/index.xml すでに作成しているアプリケーション名「kittakasample」と、そのコントローラー「YoshiMovies_」を使用する kittakasample/app/controllers/yoshi_movies_controller.rb をさくらエディタで開く =begin ファイルの保存はUTF-8でおこないます 文字列はシングルクォーテーションで囲む =end # coding: utf-8 class YoshiMoviesController < ApplicationController def index end end ----------------------------------------------------- 「kittakasample/app/views/yoshi_movies/new.html.erb」をさくらエディタで開く <h1>YoshiMovies#index</h1> <p>Find me in app/views/yoshi_movies/index.html.erb</p> ----------------------------------------------------- アプリケーションのルーティングに関する記述は「config/routes.rb」ファイルで行なう C:\SadaData\rails\kittakasample\config\routes.rb をさくらエディタで開いてみる Kittakasample::Application.routes.draw do get "yoshi_movies/index" ←前回に続きここを使用する get "ishi_photos/index" get "ishi_photos/kitshow" get "hiramemo/new" get "hiramemo/show" ----------------------------------------------------- JSON形式で出力する kittakasample/app/controllers/yoshi_movies_controller.rb をさくらエディタで開く =begin ファイルの保存はUTF-8でおこないます 文字列はシングルクォーテーションで囲む 変数_sadaPersonalへ配列を名札付ける メソッドrenderを使ってjson形式で出力する =end # coding: utf-8 class YoshiMoviesController < ApplicationController def index _sadaPersonal = {'name' => 'DrawingMother', 'old' => 2, 'Location' => '福山市', 'Zip' => 72901} render :json => _sadaPersonal end end ------------------------------------------------------ 「kittakasample」アプリケーションを実行する アプリケーションの起動 ↓アプリ名[kittakasample] C:\SadaData\rails\kittakasample>rails server ブラウザからURLへアクセス ウェブブラウザからアクセスする URL://localhost:3000/yoshi_movies/index

YoshiMovies#index

Find me in app/views/yoshi_movies/index.html.erb ------------------------------------------- ウェブブラウザからアクセスする URL://localhost:3000/yoshi_movies/index {"name":"DrawingMother","old":2,"Location":"福山市","Zip":72901} 注、画面左上に上記の文字がひっそり出力される ソースコード 1|{"name":"DrawingMother","old":2,"Location":"福山市","Zip":72901} | | ------------------------------------------- XML形式で出力 kittakasample/app/controllers/yoshi_movies_controller.rb をさくらエディタで開く =begin ファイルの保存はUTF-8でおこないます 文字列はシングルクォーテーションで囲む 変数_sadaPersonalへ配列を名札付ける メソッドrenderを使ってxml形式で出力する =end # coding: utf-8 class YoshiMoviesController < ApplicationController def index _sadaPersonal = {'name' => 'DrawingMother', 'old' => 2, 'Location' => '福山市', 'Zip' => 72901} render :xml => _sadaPersonal end end ------------------------------------------ ウェブブラウザからアクセスする URL://localhost:3000/yoshi_movies/index This XML file does not appear to have any style information associated with it. The document tree is shown below. <hash> <name>DrawingMother</name> <old type="integer">2</old> <Location>福山市</Location> <Zip type="integer">72901</Zip> </hash> 注、画面左上に雰囲気的にHTMLのようなタグが付いた形で出力される ソースコードを見てみる 1|<?xml version="1.0" encoding="UTF-8"?> 2|<hash> 3| <name>DrawingMother</name> 4| <old type="integer">2</old> 5| <Location>福山市</Location> 6| <Zip type="integer">72901</Zip> 7|</hash> 8| 9| --------------------------------------------- respond_toメソッド kittakasample/app/controllers/yoshi_movies_controller.rb をさくらエディタで開く =begin ファイルの保存はUTF-8でおこないます 文字列はシングルクォーテーションで囲む 変数@_sadaPersonalへ配列を名札付ける メソッドrespond_toを使ってjson形式とxml形式で出力する ウェブラウザから下記のようにアクセスする URL//localhost:3000/yoshi_movies/index URL//localhost:3000/yoshi_movies/index.json URL//localhost:3000/yoshi_movies/index.xml =end # coding: utf-8 class YoshiMoviesController < ApplicationController def index @_sadaPersonal = {'name' => 'DrawingMother', 'old' => 2, 'Location' => '福山市', 'Zip' => 72901} respond_to do |sadaformat| sadaformat.html sadaformat.json {render :json => @_sadaPersonal} sadaformat.xml {render :xml => @_sadaPersonal} end end end -------------------------------------------- respond_toメソッド ----------------------------------------------- =begin ファイルの保存はUTF-8でおこないます 文字列はシングルクォーテーションで囲む 変数@_sadaPersonalへ配列を名札付ける メソッドrespond_toを使ってjson形式とxml形式で出力する ウェブラウザから下記のようにアクセスする URL//localhost:3000/yoshi_movies/index URL//localhost:3000/yoshi_movies/index.json URL//localhost:3000/yoshi_movies/index.xml 配列のワードを1つ追加した。年齢を変えた =end # coding: utf-8 class YoshiMoviesController < ApplicationController def index @_sadaPersonal = {'name' => 'DrawingMother', 'old' => 1, 'Location' => '福山市', 'Zip' => 72901, '本籍' => 'Japan'} respond_to do |sadaformat| sadaformat.html sadaformat.json {render :json => @_sadaPersonal} sadaformat.xml {render :xml => @_sadaPersonal} end end end ----------------------------------------------------- 「kittakasample/app/views/yoshi_movies/new.html.erb」をさくらエディタを開き加筆する <h1>YoshiMovies#index</h1> <p> 現在地:<%= @_sadaPersonal["Location"] %><br /> 郵便番号:<%= @_sadaPersonal["Zip"] %><br /> 本籍:<%= @_sadaPersonal["本籍"] %><br /> 名前:<%= @_sadaPersonal["name"] %><br /> <!--メモ、順番入れ変えてみた エラーが出た、変数を間違っていたので保存形式ではなかった、保存形式をUTF-8に変えた--> 年齢:<%= @_sadaPersonal["old"] %><br /> <!--保存形式をEUCへ変更したらエラーとなったのでUTF-8に戻した--> </p> <p>Find me in app/views/yoshi_movies/index.html.erb</p> ------------------------------------------------------ ウェブブラウザからアクセスする時は URL://localhost:3000/yoshi_movies/index ↑コントローラ名のディレクトリ、コントローラが実行されたらビュー内のコントローラと同じディレクトリ内のファイルが実行される ブラウザから コントローラー名↓「"yoshi_movies"_controller.rb」 URL://localhost:3000/yoshi_movies/index ↓ ↑ → → → → → → → ↓ get "yoshi_movies/index" get "hiramemo/show" 説明のためだけで現在は使っていない get 'kittaka' => 'kittakashoplist#bykittakashop2019' 説明のためだけで現在は使っていない get 'sada1bye' => 'sadabooklist#sada1_bye' 説明のためだけで現在は使っていない コントロールー名↑ ↓ ↓コントローラーの index に関連付けられている 「app\controllers\yoshi_movies_controller.rb」 # coding: utf-8 class YoshiMoviesController < ApplicationController def index 配列を変数に格納する → _sadaPersonal = {'name' => 'DrawingMother', 'old' => 2, 'Location' => '福山市', 'Zip' => 72901} 変数はアンダーバーと大文字が使用できる render :json => _sadaPersonal end end メソッド↑ ↑変数を JSON形式 で出力する、テンプレートは使用しない ---------------------------------------------------------------------------------------------------------------- # coding: utf-8 class YoshiMoviesController < ApplicationController def index 配列を変数に格納する → _sadaPersonal = {'name' => 'DrawingMother', 'old' => 2, 'Location' => '福山市', 'Zip' => 72901} 変数はアンダーバーと大文字が使用できる render :xml => _sadaPersonal end end メソッド↑ ↑変数を XML形式 で出力する、テンプレートは使用しない ---------------------------------------------------------------------------------------------------------------- # coding: utf-8 class YoshiMoviesController < ApplicationController def index 配列を変数に格納する → @_sadaPersonal = {'name' => 'DrawingMother', 'old' => 1, 'Location' => '福山市', 'Zip' => 72901, '本籍' => 'Japan'} 変数はアンダーバーと大文字が使用できる respond_to do |sadaformat| sadaformat.html sadaformat.json {render :json => @_sadaPersonal} sadaformat.xml {render :xml => @_sadaPersonal} end end def show 説明のためだけで現在は使っていない end 説明のためだけで現在は使っていない end 説明のためだけで現在は使っていない ↓ ↓ ビューで出力される「app\views\yoshi_movies\index.html.erb」 ビューで出力される「app\views\Hiramemo\show.html.erb」 説明のためだけで現在は使っていない 「YoshiMovies」コントローラの「index」→メソッド「render」→文字列出力「Hello 只今コントローラの中から、、、」が呼び出されるように記述されている -----------------------------------


*↓ 参考にしたサイトは下記
こちら です。

これは赤色の文字例です。

これは青色の文字例です。