Rails4 ルーティングの基本
ルーティングを確かめるコマンド
cmdで「rake routes」コマンドを実行で確かめる
Prefix Verb URI Pattern Controller#Action blogs GET /blogs(.:format) blogs#index POST /blogs(.:format) blogs#create new_blog GET /blogs/new(.:format) blogs#new
localhost:3000/rails/info/routesでもルーティングが確認できて見やすい。
routes.rb に下記
Rails.application.routes.draw do match ':controller(/:action(/:id))', via:[:get, :post, :patch]'
end
とすると
Prefix Verb URI Pattern Controller#Action GET|POST|PATCH /:controller(/:action(/:id))(.:format) :controller#:action
とルーティング設定ができるの
こうすしてからContentsコントローラー(コントローラー名は任意)で作成して
contents_controller.rb に
Prefix Verb class ContentsController < ApplicationController
def index
render text: 'index!'
end
def show
render text: 'show!'
end
def new
render text: 'new!'
end
def edit
render text: 'edit!'
end
end
として「rails s」で
http://localhost:3000/contents/
http://localhost:3000/contents/show/4/
http://localhost:3000/contents/new/
http://localhost:3000/contents/edit/
にアクセスできるようになっておもしろかった