KAEDE Hack blog

JavaScript 中心に ライブラリなどの使い方を解説する技術ブログ。

Rails での URL-controller/action のルーティングをする

前回の復習

Puma starting in single mode...
* Version 4.3.6 (ruby 2.6.1-p33), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000

f:id:kei_s_lifehack:20201017160655p:plain

  • Rails の環境構築をホストでして、起動した

Welcome コントローラーを作る

guides.rubyonrails.org

  • この公式サイトに基づいて作業を進める
rails generate controller Welcome index

Running via Spring preloader in process 80060
      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/welcome.scss
  • rails generate controller Welcome index をすることで、
    • app/controllers/welcome_controller.rb のコントローラー
    • app/views/welcome/index.html.erb の ビュー
    • get 'welcome/index' の ルーティング
    • test, helper, scss
  • が作成された

Most important of these are of course the controller, located at app/controllers/welcome_controller.rb

and the view, located at

app/views/welcome/index.html.erb.

作成されたコントローラと ビュー を確認

class WelcomeController < ApplicationController
  def index
  end
end
  • これが controllers/welcome_controller.rb
  • Cake 見たいに、大元のAppコントローラーを拡張して作られている
<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
  • これが views/welcome/index.html.erb
  • .erb ファイルになっている

ルーティングをする

guides.rubyonrails.org

Now that we have made the controller and view, we need to tell Rails when we want "Hello, Rails!" to show up.

In our case, we want it to show up when we navigate to the root URL of our site, http://localhost:3000.

At the moment, "Yay! You're on Rails!" is occupying that spot.

  • 今コントローラーとビューは作れた
  • 私たちは Rails に "Hello, Rails!" を見せた
  • 今回のケースでは、ルート URL にアクセスした時に、 私たちが今作った ビューに アクセスさせたい
  • 今はデフォルトの "Yay! You're on Rails! がルートを占領している。

  • これを変更するために config/routes.rb を開いてみる

Rails.application.routes.draw do
  get 'welcome/index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
  • routes.rb の中身は最初はこうなっている

This is your application's routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. Edit this file by adding the line of code root 'welcome#index'. It should look something like the following:

  • Domein Spesific 言語で書かれているらしい。
  • ここにルート URL と Welcome/index を繋ぐルーティングを追加する
 root 'welcome#index'

f:id:kei_s_lifehack:20201025013844p:plain

  • これで ルート URL にアクセスしても Welcome/index の view が見えるようになった

f:id:kei_s_lifehack:20201017161721p:plain

  • welcome/index ではもともと見える。コントローラーを generate した時にルーティングが書かれている

index の省略ができない

  • 普通は hoge/index.html なら hoge/ にアクセスすれば中の index が見える
  • しかし今回の Rails App では welcome/ では welcome/index の内容は出ない。

f:id:kei_s_lifehack:20201017161820p:plain

  • 普通の index.html の仕様とは違う?

f:id:kei_s_lifehack:20201025014113p:plain

  • なお、welcome/index.html のアクセスでも出る。
  • ここは疑問だ。

ルーティングの解説

guides.rubyonrails.org

root 'welcome#index' tells Rails to map requests to the root of the application to the welcome controller's index action

and get 'welcome/index' tells Rails to map requests to http://localhost:3000/welcome/index to the welcome controller's index action.

This was created earlier when you ran the controller generator (rails generate controller Welcome index)

  • root 'welcome#index'Rails に App の root に welcome controller の index の action の 内容をマップさせる

f:id:kei_s_lifehack:20201024204708p:plain

  • 確かに root なのに welcom controller の index action が読まれている!

  • そして get 'welcome/index'Rails/welcome/index に 同じくwelcome controller の index action を map させている

  • これは以前に rails generate Controller Welcome index した時に作られている

Rails が提供する CRUD-REST メソッドの解説

guides.rubyonrails.org

Now that you've seen how to create a controller, an action, and a view, let's create something with a bit more substance.

In the Blog application, you will now create a new resource. A resource is the term used for a collection of similar objects, such as articles, people, or animals. You can create, read, update, and destroy items for a resource and these operations are referred to as CRUD operations.

  • Blog App では、新しいリソースを作る
  • リソースは似たようなオブジェクトの集まりだ。
    • 記事、人々、動物、など
  • これらの資源のアイテムを生成して、読んで、更新して、破壊する。 こういう操作を CRUD 操作と言う

Rails provides a resources method which can be used to declare a standard REST resource. You need to add the article resource to the config/routes.rb so the file will look as follows:

  • Rials はスタンダード REST リソースと呼ばれるリソースメソッドを提供する
  • 記事のリソースを config/routes.rb にこうやって追加しよう
Rails.application.routes.draw do
  get 'welcome/index'
 
  resources :articles
 
  root 'welcome#index'
end
  resources :articles
  • 空白を間違えるとエラーが出るよ

If you run rails routes, you'll see that it has defined routes for all the standard RESTful actions.

The meaning of the prefix column (and other columns) will be seen later, but for now notice that Rails has inferred the singular form article and makes meaningful use of the distinction.

  • rails routes を走らせると、全てのスタンダード REST フルな アクションが定義されるのがわかるだろう
  • 接頭語のカラムの意味は、後でやる
  • だが Rails
    • 単数の 記事の form を推測して
    • 意味のある区別を作った
  • のがわかるだろう

ルーティングの表示

rails routes
       Prefix Verb   URI Pattern                  Controller#Action
welcome_index GET    /welcome/index(.:format)     welcome#index
     articles GET    /articles(.:format)          articles#index
              POST   /articles(.:format)          articles#create
  new_article GET    /articles/new(.:format)      articles#new
 edit_article GET    /articles/:id/edit(.:format) articles#edit
      article GET    /articles/:id(.:format)      articles#show
              PATCH  /articles/:id(.:format)      articles#update
              PUT    /articles/:id(.:format)      articles#update
              DELETE /articles/:id(.:format)      articles#destroy
         root GET    /                            welcome#index
  • Cake の bake all みたいに CRUD を生成しているわけではない。

This is the "C" and the "R" from CRUD: create and read. The form for doing this will look like this:

  • こうあるが、今 /articles/index にアクセスしても何もない。
  • よく見るとコントローラーは生成されていないし、routes.rb も更新されていない
  • 今のコマンドはただ routes.rb から現在のルートを示しただけだった!
  resources :articles
  • CRUD のルーティングを生成したのはこれだ。