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

Rubyの学習 free225
resourcesメソッドを使うと8通りのルーティングの設定を行ってくれる
ただコントローラを作成してもビューのアクションは自動生成してくれないので手動で作成する

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

ルーティングを確認する(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
-----------------------------------------------------------------------------------

ブラウザからアクセス
URL://localhost:3000/sadachiblog/3/edit

Sadachiblog#edit

自作した

Find me in app/views/sadachiblog/edit.html.erb

ブラウザからアクセス URL://localhost:3000/sadachiblog/3

Sadachiblog#show

自作した

Find me in app/views/sadachiblog/show.html.erb


Tuesday,December,3,2013

プログラムの表示

MEMO アプリケーションを作る アプリケーション名 [sadachikasample2] cd C:\SadaData\rails C:\SadaData\rails>rails new sadachikasample2 コントローラの削除 コントローラ名[sada] C:\SadaData\rails\sample2>rails destroy controller sada resourcesメソッドを使ったルーティングの設定 (config/routes.rb)をさくらエディタで開く resources :sadachiblog ルーティングを確認する(config/routes.rb) C:\SadaData\rails\sample2>rake routes ------------------------------------------------ アプリケーション名 [sadachikasample2013] rails new sadachikasample2013 C:\SadaData\rails>rails new sadachikasample2013 cd C:\SadaData\rails\sadachikasample2013 コントローラーを作る コントローラ名「testsada」アクション名「index」「new」「edit」「show」を作成 C:\SadaData\rails\sadachikasample2013>rails generate controller testsada index new edit show (app/controllers/testsada_controller.rb)が作成された さくらエディタで開いて見る class TestsadaController < ApplicationController def index end def new end def edit end def show end end (app/views/testsada)を開く、ビューが作成さた edit.html.erb index.html.erb new.html.erb show.html.erb ルーティングを確認する(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 (config/routes.rb)をさくらエディタで開く Sadachikasample2013::Application.routes.draw do get "testsada/index" get "testsada/new" get "testsada/edit" get "testsada/show" end 「sadachikasample2013」アプリケーションを実行する アプリケーションの起動 C:\SadaData\rails\sadachikasample2013>rails server ブラウザからアクセスしてみる URL://localhost:3000/testsada/ ERROR URL://localhost:3000/testsada/index 表示された URL://localhost:3000/testsada/new 表示された URL://localhost:3000/testsada/edit 表示された URL://localhost:3000/testsada/show 表示された resourcesメソッドを使ってルーティングを設定 (config/routes.rb)をさくらエディタで開く Sadachikasample2013::Application.routes.draw do get "testsada/index" get "testsada/new" get "testsada/edit" get "testsada/show" end 以下に加筆する Sadachikasample2013::Application.routes.draw do resources :sadachiblog #コメントテストここを加筆することでルーティングを8つ設定する。 get "testsada/index" get "testsada/new" get "testsada/edit" get "testsada/show" end ルーティングを確認する(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 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 ブラウザからアクセスしてみる URL://localhost:3000/sadachiblog/ ERROR URL://localhost:3000/sadachiblog/index ERROR URL://localhost:3000/sadachiblog/new ERROR URL://localhost:3000/sadachiblog/edit ERROR URL://localhost:3000/sadachiblog/show ERROR URL://localhost:3000/testsada/ ERROR URL://localhost:3000/testsada/index 表示された URL://localhost:3000/testsada/new 表示された URL://localhost:3000/testsada/edit 表示された URL://localhost:3000/testsada/show 表示された (config/routes.rb)をさくらエディタで開く Sadachikasample2013::Application.routes.draw do resources :sadachiblog #コメントテストここを加筆することでルーティングを8つ設定する。 get "testsada/index" ←削除する get "testsada/new" ←削除する get "testsada/edit" ←削除する get "testsada/show" ←削除する end ルーティングを確認する(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 ブラウザからアクセスしてみる URL://localhost:3000/sadachiblog/ ERROR URL://localhost:3000/sadachiblog/index ERROR URL://localhost:3000/sadachiblog/new ERROR URL://localhost:3000/sadachiblog/edit ERROR URL://localhost:3000/sadachiblog/show ERROR URL://localhost:3000/testsada/ ERROR URL://localhost:3000/testsada/index ERROR URL://localhost:3000/testsada/new ERROR URL://localhost:3000/testsada/edit ERROR URL://localhost:3000/testsada/show ERROR class TestsadaController < ApplicationController def index end def new end def edit end def show end end (app/views/testsada)をコピーしてフォルダ名を変える→(app/views/sadachiblog)→コントローラの作成時にビューが作成されるか確認するため (app/views/sadachiblog_0)一時的にフォルダの名前を変えた ブラウザからアクセスしてみる URL://localhost:3000/sadachiblog/ ERROR URL://localhost:3000/sadachiblog/index ERROR URL://localhost:3000/sadachiblog/new ERROR URL://localhost:3000/sadachiblog/edit ERROR URL://localhost:3000/sadachiblog/show ERROR URL://localhost:3000/testsada/ ERROR URL://localhost:3000/testsada/index ERROR URL://localhost:3000/testsada/new ERROR URL://localhost:3000/testsada/edit ERROR URL://localhost:3000/testsada/show ERROR コントローラがなかった コントローラーを作る コントローラ名「sadachiblog」アクション名「index」「new」「edit」「show」を作成 rails generate controller sadachiblog index new edit show C:\SadaData\rails\sadachikasample2013>rails generate controller sadachiblog index new edit show (app/controllers/sadachiblog_controller.rb)が作成された さくらエディタで開いて見る class SadachiblogController < ApplicationController def index end def new end def edit end def show end end (app/views)を開くが、ビューは作成されていない しかたないので手動で作る # coding: utf-8 <h1>Sadachiblog#index</h1> <a>自作した</a> <p>Find me in app/views/sadachiblog/index.html.erb</p> # coding: utf-8 <h1>Sadachiblog#new</h1> <a>自作した</a> <p>Find me in app/views/sadachiblog/new.html.erb</p> URL://localhost:3000/sadachiblog/3/edit # coding: utf-8 <h1>Sadachiblog#edit</h1> <a>自作した</a> <p>Find me in app/views/sadachiblog/edit.html.erb</p> URL://localhost:3000/sadachiblog/3 # coding: utf-8 <h1>Sadachiblog#show</h1> <a>自作した</a> <p>Find me in app/views/sadachiblog/show.html.erb</p> ブラウザからアクセスしてみる URL://localhost:3000/sadachiblog/ 表示された URL://localhost:3000/sadachiblog/index 表示された URL://localhost:3000/sadachiblog/new 表示された URL://localhost:3000/sadachiblog/edit 表示された URL://localhost:3000/sadachiblog/show 表示された URL://localhost:3000/sadachiblog/3 表示された URL://localhost:3000/sadachiblog/5/edit 表示された 下記の部分 sadachiblog GET /sadachiblog/:id(.:format) sadachiblog#show edit_sadachiblog GET /sadachiblog/:id/edit(.:format) sadachiblog#edit URL://localhost:3000/testsada/ ERROR URL://localhost:3000/testsada/index ERROR URL://localhost:3000/testsada/new ERROR URL://localhost:3000/testsada/edit ERROR URL://localhost:3000/testsada/show ERROR 自動で8個のルーティングが作成されるとはいえ、ビューは自作とはしんどいな 意味のないコントローラを削除 C:\SadaData\rails\sample2013>rails destroy controller testsada コントローラは消えた(app/controllers/testsada_controller.rb) ビューも消えた(app/views/testsada) resourcesメソッドを使うとコントローラ作成時にビューが作成されなくなるのはなぜ?


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

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

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