0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails config/routes.rbの基本

Last updated at Posted at 2019-08-04

1. root

あるドメイン名(例えばlocalhost:3000)でアクセスしたら、指定したcontroller, action, viewに従いブラウザに表示する。
ひとつのドメインに対して一つのrootのみ

config/routes.rb
Rails.application.routes.draw do
  root 'users#index'
end

view : users/index.html.erb
controller : Users
action : index
url欄の表示 : localhost:3000
root_path = 'localhost:3000'
(root_pathは変数として使える)

urlを打ち込みブラウザに表示するまでの流れ
1 「localhost:3000」をurlで検索する
2 config/routes.rbから該当するcontroller, action, viewを読み込む
3 読み込んだcontroller(Users), action(index)で「@name='hoge'」が入る
4 読み込んだviewでcontroller, actionで設定した「@name」をブラウザ上で表示させる

1 「localhost:3000」

2      __________________
      |                  |     controller : Users
      |root 'users#index'|  →  action     : index
      |                  |     view       : users/index.html.erb
      |__________________|
       config/routes.rb

3      _____________         
      |             |       
      |def index    |          
      | @name='hoge'|       → @name = 'hoge'
      |end          |        
      |             |        
      |_____________|         
app/controllers/users_controller.rb

4      ____________
      |<%= @name %>|
      |            |
      |            |
      |            |
      |            |
      |____________|
app/views/users/index.html.erb
       ____________
      | hoge       | 
      |            |
      |            |
      |            |
      |            |
      |____________|
     ブラウザ上の表示結果

2. get

指定したview,controller,actionに従いブラウザに表示する。
rootと違い、urlの表示が「ドメイン名/???」となり、???を変えれば複数の設定が可能

config/routes.rb
Rails.application.routes.draw do
  get '/index', to: 'users#index'
end

view : users/index.html.erb
controller : Users
action : index
url欄の表示 : localhost:3000/index
index_path = 'localhost:3000/index'
(index_pathは変数として使える)

urlを打ち込みブラウザに表示するまでの流れ(ドメインの後ろに「/???」がつく所などがrootと異なる)
1 「localhost:3000/index」をurlで検索する
2 config/routes.rbから該当するcontroller, action, viewを読み込む
3 読み込んだcontroller(Users), action(index)で「@name='hoge'」が入る
4 読み込んだviewでcontroller, actionで設定した「@name」をブラウザ上で表示させる

1 「localhost:3000/index」

2      _______________________________
      |                               |     controller : Users
      |get '/index', to: 'users#index'|  →  action     : index
      |                               |     view       : users/index.html.erb
      |_______________________________|
             config/routes.rb

3      _____________         
      |             |       
      |def index    |          
      | @name='hoge'|       → @name = 'hoge'
      |end          |        
      |             |        
      |_____________|         
app/controllers/users_controller.rb

4      ____________
      |<%= @name %>|
      |            |
      |            |
      |            |
      |            |
      |____________|
app/views/users/index.html.erb
       ____________
      | hoge       | 
      |            |
      |            |
      |            |
      |            |
      |____________|
     ブラウザ上の表示結果

3. post

「form_for」などでdbに新規データを保存する時に使う。(違う事を言っているかもしれません)

config/routes.rb
Rails.application.routes.draw do
  get '/users/new', to: 'users#new'
  post '/users', to: 'users#create'
end
app/controllers/users_controller.rb
class UsersController < ApplicationController
  def new
    @user = User.new
  end
  def create
    user = User.new(user_params)
    #dbはUserテーブルでカラムがname(string), age(integer)を用意しておく
    user.save
    #本当はdbに保存する前に色々と値が正しいのかチェックしたいが今回は省略する
  end

  private

    def user_params
      params.require(:user).permit(:name, :age)
    end
  end
end
app/views/users/new.html.erb
<%= form_for @user do |f| %>
  <%= f.label :名前 %>
  <%= f.text_field :name %>
  <%= f.label :年齢 %>
  <%= f.number_field :age %>
  <%= f.submit "新規作成" %>
<% end %>
form_forなどでdbに新規データを保存するまでの流れ
1 「localhost:3000/users/new」に移動
2 config/routes.rbから該当するgetのcontroller, action, viewを読み込む
3 getなどでform入力画面に移動(newメソッドで「@user」を作成、viewで使用する)
4 「form_for」などでフォームにデータを入力しsubmitボタンを押す(「@user」にデータを入れて投げる)
5 config/routes.rbから該当するpostのcontroller, action, (view)を読み込む
6 読み込んだcontroller(Users), action(create)に従い、投げられた「@user」データをdbに保存する

1 「localhost:3000/users/new」

2      _________________________________
      |                                 |     controller : Users
      |get '/users/new', to: 'users#new'|  →  action     : new
      |                                 |     view       : users/new.html.erb
      |_________________________________|
             config/routes.rb

3      _______________         
      |               |       
      |def new        |          
      | @user=User.new|       → @user = User.new
      |end            |        
      |               |        
      |_______________|         
app/controllers/users_controller.rb

       __________________
      |form_for          |
      |上を参照           |       ← @user = User.new
      |name:             |
      |age:              |   
      |                  |
      |__________________|
   app/views/users/new.html.erb

4      __________________
      |form_for          |
      |上を参照           |
      |name:'yamada taro'|
      |age:105           |       → @user.name = 'yamada taro'    
      |                  |     @user.age = 105
      |__________________|
   app/views/users/new.html.erb

理解していないのだが、form_forで飛ぶurlはデフォルトだと'localhos:3000/users'になるのかな
一応form_forのオプションでurlを指定も出来る。
今回は理解が出来ていないが、app/views/users/new.html.erbのform_forで飛ぶ先は'localhost:3000/users'という事で進める。

5      _________________________________
      |                                 |     controller : Users
      |post '/users', to: 'users#create'|  →  action     : create
      |                                 |     view       : users/new.html.erb
      |_________________________________|
             config/routes.rb

6      _______________         
      |               |       
      |def create     |          
      |  上を参照      |       ← @user.name = 'yamada taro'
      |end            |         @user.age = 105
      |               |        
      |_______________|         
app/controllers/users_controller.rb
           ↓
       @user.name = 'yamada taro'
       @user.age = 105
       ____↓_____
      | / / / / /|
      |/ / / / / |
      | / / / / /|
      |/ / / / / |  
      |_/_/_/_/_/|
           db

4. patch

dbにあるデータを変更し保存する

5. delete

dbにあるデータを削除する

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?