1. root
あるドメイン名(例えばlocalhost:3000)でアクセスしたら、指定したcontroller, action, viewに従いブラウザに表示する。
ひとつのドメインに対して一つのrootのみ
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は変数として使える)
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の表示が「ドメイン名/???」となり、???を変えれば複数の設定が可能
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は変数として使える)
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に新規データを保存する時に使う。(違う事を言っているかもしれません)
Rails.application.routes.draw do
get '/users/new', to: 'users#new'
post '/users', to: 'users#create'
end
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
<%= form_for @user do |f| %>
<%= f.label :名前 %>
<%= f.text_field :name %>
<%= f.label :年齢 %>
<%= f.number_field :age %>
<%= f.submit "新規作成" %>
<% end %>
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にあるデータを削除する