#ルーティングの追加
web/router.exに記載していきます。
defmodule Sample.Router do
use Sample.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Sample do
pipe_through :browser # Use the default browser stack
# ここに書いていく
end
# Other scopes may use custom stacks.
scope "/api", Sample do
pipe_through :api
# apiとかはこっち
end
end
get
get "users", UserController, :index
# get {ルーティング},{コントローラー},{アクション}
# $ mix phoenix.routes
user_path GET /users Sample.UserController :index
post
post "users/new", UserController, :new
# $ mix phoenix.routes
user_path POST /users Sample.UserController :new
##resources
getとかpostとか書いてくのがめんどくさいって思ったら
resourcesを使うと便利です
resources "/users", UserController
# $ mix phoenix.routes
user_path GET /users Sample.UserController :index
user_path GET /users/:id/edit Sample.UserController :edit
user_path GET /users/new Sample.UserController :new
user_path GET /users/:id Sample.UserController :show
user_path POST /users Sample.UserController :create
user_path PATCH /users/:id Sample.UserController :update
PUT /users/:id Sample.UserController :update
user_path DELETE /users/:id Sample.UserController :delete
のように一行で複数のルーティングを定義できます。
また、mix phoenix.gen.html
でコントローラーなどを生成した場合は
resourcesで定義されるルーティングに関連するアクションやテンプレートなどが生成されるので
これを使うのが多くなるのかなぁって印象です。
resourcesでも特定のルーティングは除外したいや特定のルーティングのみ使いたい場合は
# 特定のルーティングのみ指定
resources "/users", UserController, only: [:new, :create]
# $ mix phoenix.routes
user_path GET /users/new Sample.UserController :new
user_path POST /users Sample.UserController :create
# 特定のルーティングを除外
resources "/users", UserController, except: [:new, :create]
# $ mix phoenix.routes
user_path GET /users Sample.UserController :index
user_path GET /users/:id/edit Sample.UserController :edit
user_path GET /users/:id Sample.UserController :show
user_path PATCH /users/:id Sample.UserController :update
PUT /users/:id Sample.UserController :update
user_path DELETE /users/:id Sample.UserController :delete
ルーティングのネスト
resources "/users", UserController do
resources "/posts", PostController
end
# $ mix phoenix.routes
user_path GET /users Sample.UserController :index
user_path GET /users/:id/edit Sample.UserController :edit
user_path GET /users/new Sample.UserController :new
user_path GET /users/:id Sample.UserController :show
user_path POST /users Sample.UserController :create
user_path PATCH /users/:id Sample.UserController :update
PUT /users/:id Sample.UserController :update
user_path DELETE /users/:id Sample.UserController :delete
user_post_path GET /users/:user_id/posts Sample.PostController :index
user_post_path GET /users/:user_id/posts/:id/edit Sample.PostController :edit
user_post_path GET /users/:user_id/posts/new Sample.PostController :new
user_post_path GET /users/:user_id/posts/:id Sample.PostController :show
user_post_path POST /users/:user_id/posts Sample.PostController :create
user_post_path PATCH /users/:user_id/posts/:id Sample.PostController :update
PUT /users/:user_id/posts/:id Sample.PostController :update
user_post_path DELETE /users/:user_id/posts/:id Sample.PostController :delete
管理画面のように先頭に/adminみたいのをつけたいとき
scope "/admin", as: :admin do
pipe_through :browser # Use the default browser stack
resources "/users", UserController, except: [:new, :create]
end
# $ mix phoenix.routes
admin_user_path GET /admin/users UserController :index
admin_user_path GET /admin/users/:id/edit UserController :edit
admin_user_path GET /admin/users/:id UserController :show
admin_user_path PATCH /admin/users/:id UserController :update
PUT /admin/users/:id UserController :update
admin_user_path DELETE /admin/users/:id UserController :delete
as: :admin
の部分のadminを変えるとadmin_user_pathの名前も変わります
APIとかにバージョンを持たせたいとき
scope "/api", as: :api do
pipe_through :api
scope "/v1", V1, as: :v1 do
resources "/users", UserController, only: [:new, :create]
end
end
# $ mix phoenix.routes
api_v1_user_path GET /api/v1/users/new V1.UserController :new
api_v1_user_path POST /api/v1/users V1.UserController :create
#リンク
1.elixir on phoenix セットアップ
2.elixir on phoenix Mix Tasksについて
3.elixir on phoenix ectoを使っていろいろなSELECT文
4.elixir on phoenix ルーティングの書き方についてまとめてみた
5.elixir on phoenix changesetでのValidates