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

Rubyの学習 free226
[resources]メソッドを使うと8通りのルーティングの設定を行ってくれる
その設定に後からアクションを付け足す方法を学習した。

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

C:\SadaData\rails\sadachikasample2013>rake routes
                 Prefix Verb   URI Pattern                       Controller#Action
     preview_hirayablog GET    /hirayablog/:id/preview(.:format) hirayablog#preview  ←ここが新しく追加されたアクション
       reset_hirayablog POST   /hirayablog/:id/reset(.:format)   hirayablog#reset    ←ここが新しく追加されたアクション
search_hirayablog_index GET    /hirayablog/search(.:format)      hirayablog#search   ←ここが新しく追加されたアクション
       hirayablog_index GET    /hirayablog(.:format)             hirayablog#index
                        POST   /hirayablog(.:format)             hirayablog#create
         new_hirayablog GET    /hirayablog/new(.:format)         hirayablog#new
        edit_hirayablog GET    /hirayablog/:id/edit(.:format)    hirayablog#edit
             hirayablog GET    /hirayablog/:id/(.:format)        hirayablog#show
                        PATCH  /hirayablog/:id(.:format)         hirayablog#update
                        PUT    /hirayablog/:id(.:format)         hirayablog#update
                        DELET  /hirayablog/:id(.:format)         hirayablog#destroy

「sadachikasample2013」アプリケーションを実行する
アプリケーションの起動
C:\SadaData\rails\sadachikasample2013>rails server

URL://localhost:3000/hirayablog/18/preview 
# coding: utf-8

Hirayablog#edit

自作した Find me in app/views/hirayablog/edit.html.erb URL://localhost:3000/hirayablog/search # coding: utf-8

Hirayablog#index

自作した Find me in app/views/hirayablog/index.html.erb URL://localhost:3000/hirayablog # coding: utf-8

Hirayablog#new

自作した Find me in app/views/hirayablog/new.html.erb URL://localhost:3000/hirayablog/new # coding: utf-8

Hirayablog#preview

自作した Find me in app/views/hirayablog/preview.html.erb URL://localhost:3000/hirayablog/18/edit # coding: utf-8

Hirayablog#search

自作した Find me in app/views/hirayablog/search.html.erb URL://localhost:3000/hirayablog/18/ # coding: utf-8

Hirayablog#show

自作した Find me in app/views/hirayablog/show.html.erb

Thursday,December,5,2013

プログラムの表示

resources :リソース名 do member do HTTPメソッド名 'アクション名' end collection do HTTPメソッド名 'アクション名' end end member ブロックは特定のデータを対象としたアクションを記述する(アクションに対してはリクエストでIDパラメータを指定する必要) collection ブロックは全てのデータを対象としたアクションを記述 --------------------------------------------------------------------------- あたらしく始めようかと思ったら前回のコードが残っていたので コントローラやルーティングなどの削除が必要になった その記録も残す。 ルーティングを確認する(config/routes.rb) コマンドプトンプトへ打ち込む C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action testsada_index GET /testsada/index(.:format) testsada#index tesutsada_new GET /tesutsada/new(.:format) testsada#new testsada_edit GET /testsada/edit(.:format) testsada#edit testsada_show GET /testsada/show(.:format) testsada#show sadachiblog_index GET /sadachiblog(.:format) sadachiblog#index POST /sadachiblog(.:format) sadachiblog#create new_sadachiblog GET /sadachiblog/new(.:format) sadachiblog#new edit_sadachiblog GET /sadachiblog/:id/edit(.:format) sadachiblog#edit sadachiblog GET /sadachiblog/:id(.:format) sadachiblog#show PATCH /sadachiblog/:id(.:format) sadachiblog#update PUT /sadachiblog/:id(.:format) sadachiblog#updete DELETE /sadachiblog/:id(.:format) sadachiblog#destroy 意味のないコントローラを削除 C:\SadaData\rails\sample2013>rails destroy controller testsada コントローラは消えた(app/controllers/testsada_controller.rb) ビューも消えた(app/views/testsada) ルーティングを確認する(config/routes.rb) 残っているね C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action testsada_index GET /testsada/index(.:format) testsada#index tesutsada_new GET /tesutsada/new(.:format) testsada#new testsada_edit GET /testsada/edit(.:format) testsada#edit testsada_show GET /testsada/show(.:format) testsada#show sadachiblog_index GET /sadachiblog(.:format) sadachiblog#index POST /sadachiblog(.:format) sadachiblog#create new_sadachiblog GET /sadachiblog/new(.:format) sadachiblog#new edit_sadachiblog GET /sadachiblog/:id/edit(.:format) sadachiblog#edit sadachiblog GET /sadachiblog/:id(.:format) sadachiblog#show PATCH /sadachiblog/:id(.:format) sadachiblog#update PUT /sadachiblog/:id(.:format) sadachiblog#updete DELETE /sadachiblog/:id(.:format) sadachiblog#destroy (config/routes.rb)さくらエディタで開く Sadachikasample2013::Application.routes.draw do get "testsada/index" ←使わないので削除する(コントローラとビューを削除してもルーティングは残っている) get "testsada/new" ←使わないので削除する get "testsada/edit" ←使わないので削除する get "testsada/show" ←使わないので削除する resources :sadachiblog #コメントテストここを加筆することでルーティングを8つ設定する。 ルーティングを確認する(config/routes.rb) コマンドプロントで確認する C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action sadachiblog_index GET /sadachiblog(.:format) sadachiblog#index POST /sadachiblog(.:format) sadachiblog#create new_sadachiblog GET /sadachiblog/new(.:format) sadachiblog#new edit_sadachiblog GET /sadachiblog/:id/edit(.:format) sadachiblog#edit sadachiblog GET /sadachiblog/:id(.:format) sadachiblog#show PATCH /sadachiblog/:id(.:format) sadachiblog#update PUT /sadachiblog/:id(.:format) sadachiblog#updete DELETE /sadachiblog/:id(.:format) sadachiblog#destroy 「sadachiblog」とそのビューも削除する。(ビューよそにコピーを取っておく) C:\SadaData\rails\sample2013>rails destroy controller sadachiblog コントローラは消えた(app/controllers/testsada_controller.rb) ビューも消えた(app/views/testsada) ルーティングを確認する(config/routes.rb) コマンドプロンプト C:\SadaData\rails\sadachikasample2013>rake routes 残ってる Prefix Verb URI Pattern Controller#Action sadachiblog_index GET /sadachiblog(.:format) sadachiblog#index ←使わないので削除する(コントローラとビューを削除してもルーティングは残っている) POST /sadachiblog(.:format) sadachiblog#create ←使わないので削除する new_sadachiblog GET /sadachiblog/new(.:format) sadachiblog#new ←使わないので削除する edit_sadachiblog GET /sadachiblog/:id/edit(.:format) sadachiblog#edit ←使わないので削除する sadachiblog GET /sadachiblog/:id(.:format) sadachiblog#show ←使わないので削除する PATCH /sadachiblog/:id(.:format) sadachiblog#update ←使わないので削除する PUT /sadachiblog/:id(.:format) sadachiblog#updete ←使わないので削除する DELETE /sadachiblog/:id(.:format) sadachiblog#destroy ←使わないので削除する (config/routes.rb)さくらエディタで開く Sadachikasample2013::Application.routes.draw do resources :sadachiblog #コメントテストここを加筆することでルーティングを8つ設定する。 ←使わないので削除する(コントローラとビューを削除してもルーティングは残っている) ルーティングを確認する(config/routes.rb) コマンドプロンプト C:\SadaData\rails\sadachikasample2013>rake routes 消えたね You don't have any routes defined! ここから始めますよ 先ほどの「resources」メソッドを使うと8通りのルーティングの設定がおこなえました それにプラス3つのアクションを加えて11のアクションをルーティングに設定をおこないます (config/routes.rb)さくらエディタで開く[resources]メソッドを使うと8通りのルーティングの設定を行ってくれる Sadachikasample2013::Application.routes.draw do resources :sadachiblog do #コントローラ名「sadachiblog」 member do #この書き方で、先ほどの8のアクションと新たに加わる3つのアクションが作成されます get 'preview' #「preview」先ほど削除したアクションには含まれていなかったアクション名です post 'reset' #「reset」 先ほど削除したアクションには含まれていなかったアクション名です end collection do get 'search' #「search」 先ほど削除したアクションには含まれていなかったアクション名です end end end #member ブロックは特定のデータを対象としたアクションを記述する(アクションに対してはリクエストでIDパラメータを指定する必要) #collection ブロックは全てのデータを対象としたアクションを記述 ルーティングを確認する(config/routes.rb) コマンドプロンプト C:\SadaData\rails\sadachikasample2013>rake routes ルーティングを確認する(config/routes.rb) コマンドプロンプト C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action preview_sadachiblog GET /sadachiblog/:id/preview(.:format) sadachiblig#preview ←ここが新しく追加されたアクション reset_sadachiblog POST /sadachiblog/:id/reset(.:format) sadachiblog#reset ←ここが新しく追加されたアクション search_sadachiblog_index GET /sadachiblog/search(.:format) sadachiblog#search ←ここが新しく追加されたアクション sadachiblog_index GET /sadachiblog(.:format) sadachiblog#index POST /sadachiblog(.:format) sadachiblog#create new_sadachiblog GET /sadachiblog/new(.:format) sadachiblog#new edit_sadachiblog GET /sadachiblog/:id/edit(.:format) sadachiblog#edit sadachiblog GET /sadachiblog/:id/(.:format) sadachiblog#show PATCH /sadachiblog/:id(.:format) sadachiblog#update PUT /sadachiblog/:id(.:format) sadachiblog#update DELET /sadachiblog/:id(.:format) sadachiblog#destroy ----------------------------------------------------- (config/routes.rb)さくらエディタで開くコントローラ名を「hirayablog」に変更してみる。(コントローラもビューもまだ作ってないからね) Sadachikasample2013::Application.routes.draw do resources :hirayablog do #コントローラ名「hirayablog」 member do #この書き方で、先ほどの8のアクションと新たに加わる3つのアクションが作成されます get 'preview' #「preview」先ほど削除したアクションには含まれていなかったアクション名です post 'reset' #「reset」 先ほど削除したアクションには含まれていなかったアクション名です end collection do get 'search' #「search」 先ほど削除したアクションには含まれていなかったアクション名です end end end ルーティングを確認する(config/routes.rb) コマンドプロンプト 「hirayablog」に変わったね C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action preview_hirayablog GET /hirayablog/:id/preview(.:format) hirayablog#preview ←ここが新しく追加されたアクション reset_hirayablog POST /hirayablog/:id/reset(.:format) hirayablog#reset ←ここが新しく追加されたアクション search_hirayablog_index GET /hirayablog/search(.:format) hirayablog#search ←ここが新しく追加されたアクション hirayablog_index GET /hirayablog(.:format) hirayablog#index POST /hirayablog(.:format) hirayablog#create new_hirayablog GET /hirayablog/new(.:format) hirayablog#new edit_hirayablog GET /hirayablog/:id/edit(.:format) hirayablog#edit hirayablog GET /hirayablog/:id/(.:format) hirayablog#show PATCH /hirayablog/:id(.:format) hirayablog#update PUT /hirayablog/:id(.:format) hirayablog#update DELET /hirayablog/:id(.:format) hirayablog#destroy コントローラーを作る コントローラ名「hirayablog」アクション名「index」「new」「edit」「show」「preview」「search」を作成 C:\SadaData\rails\sadachikasample2013>rails generate controller hirayablog index new edit show preview search (app/controllers/hirayablog_controller.rb)が作成された さくらエディタで開いて見る class HirayablogController < ApplicationController def index end def new end def edit end def show end def preview end def search end end (app/views/)を開く、ビューは作成されていない しかたないので自作した (app/views/hirayablog) edit.html.erb index.html.erb new.html.erb preview.html.erb search.html.erb show.html.erb # coding: utf-8 <h1>Hirayablog#edit</h1> <a>自作した</a> <p>Find me in app/views/hirayablog/edit.html.erb</p> # coding: utf-8 <h1>Hirayablog#index</h1> <a>自作した</a> <p>Find me in app/views/hirayablog/index.html.erb</p> # coding: utf-8 <h1>Hirayablog#new</h1> <a>自作した</a> <p>Find me in app/views/hirayablog/new.html.erb</p> # coding: utf-8 <h1>Hirayablog#preview</h1> <a>自作した</a> <p>Find me in app/views/hirayablog/preview.html.erb</p> # coding: utf-8 <h1>Hirayablog#search</h1> <a>自作した</a> <p>Find me in app/views/hirayablog/search.html.erb</p> # coding: utf-8 <h1>Hirayablog#show</h1> <a>自作した</a> <p>Find me in app/views/hirayablog/show.html.erb</p> 「sadachikasample2013」アプリケーションを実行する アプリケーションの起動 C:\SadaData\rails\sadachikasample2013>rails server ブラウザからURLへアクセス URL://localhost:3000/hirayablog/18/preview 表示された「preview」 URL://localhost:3000/hirayablog/search 表示された「search」 URL://localhost:3000/hirayablog 表示された「index」 URL://localhost:3000/hirayablog/new 表示された「new」 URL://localhost:3000/hirayablog/18/edit 表示された「edit」 URL://localhost:3000/hirayablog/18/ 表示された「show」 下記の設定にしたがっている preview_hirayablog GET /hirayablog/:id/preview(.:format) hirayablog#preview ←ここが新しく追加されたアクション search_hirayablog_index GET /hirayablog/search(.:format) hirayablog#search ←ここが新しく追加されたアクション hirayablog_index GET /hirayablog(.:format) hirayablog#index new_hirayablog GET /hirayablog/new(.:format) hirayablog#new edit_hirayablog GET /hirayablog/:id/edit(.:format) hirayablog#edit hirayablog GET /hirayablog/:id/(.:format) hirayablog#show 別の記述方法 resources :リソース名 do HTTPメソッド名 'アクション名', :on => :member HTTPメソッド名 'アクション名', :on => :collection end 「hirayablog」コントローラを削除 C:\SadaData\rails\sample2013>rails destroy controller hirayablog コントローラは消えた(app/controllers/hirayablog_controller.rb) ビューも消えた(app/views/hirayablog)(自作してても削除された、作ってくれないのに削除だけはしてくれる) ルーティングを確認する(config/routes.rb) コマンドプロンプト 「hirayablog」が残っている C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action preview_hirayablog GET /hirayablog/:id/preview(.:format) hirayablog#preview reset_hirayablog POST /hirayablog/:id/reset(.:format) hirayablog#reset search_hirayablog_index GET /hirayablog/search(.:format) hirayablog#search hirayablog_index GET /hirayablog(.:format) hirayablog#index POST /hirayablog(.:format) hirayablog#create new_hirayablog GET /hirayablog/new(.:format) hirayablog#new edit_hirayablog GET /hirayablog/:id/edit(.:format) hirayablog#edit hirayablog GET /hirayablog/:id/(.:format) hirayablog#show PATCH /hirayablog/:id(.:format) hirayablog#update PUT /hirayablog/:id(.:format) hirayablog#update DELET /hirayablog/:id(.:format) hirayablog#destroy (config/routes.rb)さくらエディタで開く Sadachikasample2013::Application.routes.draw do resources :hirayablog do ←削除する member do ←削除する get 'preview' ←削除する post 'reset' ←削除する end ←削除する ←削除する collection do ←削除する get 'search' ←削除する end ←削除する end ←削除する end #member ブロックは特定のデータを対象としたアクションを記述する(アクションに対してはリクエストでIDパラメータを指定する必要) #collection ブロックは全てのデータを対象としたアクションを記述 ルーティングを確認する(config/routes.rb) コマンドプロンプト 消えたね C:\SadaData\rails\sadachikasample2013>rake routes You don't have any routes defined! (config/routes.rb)さくらエディタで開く コントローラ名を「kittablog」とする。これで自動的に8個のアクションと下記の3個のアクションがルーティングに記載される(コントローラとビューの作成は別の作業が必要)。 Sadachikasample2013::Application.routes.draw do resources :kittablog do get 'preview', :on => :member post 'reset', :on => :member get 'search', :on => :collection end end #member ブロックは特定のデータを対象としたアクションを記述する(アクションに対してはリクエストでIDパラメータを指定する必要) #collection ブロックは全てのデータを対象としたアクションを記述 ルーティングを確認する(config/routes.rb) コマンドプロンプト C:\SadaData\rails\sadachikasample2013>rake routes Prefix Verb URI Pattern Controller#Action preview_kittablog GET /kittablog/:id/preview(.:format) kittablog#preview reset_kittablog POST /kittablog/:id/reset(.:format) kittablog#reset search_kittablog_index GET /kittablog/search(.:format) kittablog#search kittablog_index GET /kittablog(.:format) kittablog#index POST /kittablog(.:format) kittablog#create new_kittablog GET /kittablog/new(.:format) kittablog#new edit_kittablog GET /kittablog/:id/edit(.:format) kittablog#edit kittablog GET /kittablog/:id/(.:format) kittablog#show PATCH /kittablog/:id(.:format) kittablog#update PUT /kittablog/:id(.:format) kittablog#update DELET /kittablog/:id(.:format) kittablog#destroy コントローラーを作る コントローラ名「kittablog」アクション名「index」「new」「edit」「show」「preview」「search」を作成 C:\SadaData\rails\sadachikasample2013>rails generate controller kittablog index new edit show preview search (app/controllers/kittablog_controller.rb)が作成された さくらエディタで開いて見る class KittablogController < ApplicationController def index end def new end def edit end def show end def preview end def search end end (app/views/)を開く、ビューは作成されていない しかたないので自作した (app/views/Kittablog) edit.html.erb index.html.erb new.html.erb preview.html.erb search.html.erb show.html.erb # coding: utf-8 <h1>Kittablog#edit</h1> <a>自作した</a> <p>Find me in app/views/kittablog/edit.html.erb</p> # coding: utf-8 <h1>Kittablog#index</h1> <a>自作した</a> <p>Find me in app/views/kittablog/index.html.erb</p> # coding: utf-8 <h1>Kittablog#new</h1> <a>自作した</a> <p>Find me in app/views/kittablog/new.html.erb</p> # coding: utf-8 <h1>Kittablog#preview</h1> <a>自作した</a> <p>Find me in app/views/kittablog/preview.html.erb</p> # coding: utf-8 <h1>Kittablog#search</h1> <a>自作した</a> <p>Find me in app/views/kittablog/search.html.erb</p> # coding: utf-8 <h1>Kittablog#show</h1> <a>自作した</a> <p>Find me in app/views/kittablog/show.html.erb</p> ブラウザからURLへアクセス URL://localhost:3000/kittablog/18/preview 表示された「preview」 URL://localhost:3000/kittablog/search 表示された「search」 URL://localhost:3000/kittablog 表示された「index」 URL://localhost:3000/kittablog/new 表示された「new」 URL://localhost:3000/kittablog/18/edit 表示された「edit」 URL://localhost:3000/kittablog/18/ 表示された「show」 下記の設定にしたがっている preview_kittablog GET /kittablog/:id/preview(.:format) kittablog#preview ←GETの部分だけテストする search_kittablog_index GET /kittablog/search(.:format) kittablog#search kittablog_index GET /kittablog(.:format) kittablog#index new_kittablog GET /kittablog/new(.:format) kittablog#new edit_kittablog GET /kittablog/:id/edit(.:format) kittablog#edit kittablog GET /kittablog/:id/(.:format) kittablog#show


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

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

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