elixir on phoenix ルーティングの書き方についてまとめてみた


#ルーティングの追加
web/router.exに記載していきます。

```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

```ex
get "users", UserController, :index

# get {ルーティング},{コントローラー},{アクション}

# $ mix phoenix.routes  
user_path  GET   /users  Sample.UserController :index
```

## post

```ex
post "users/new", UserController, :new

# $ mix phoenix.routes  
user_path  POST  /users  Sample.UserController :new

```

##resources
getとかpostとか書いてくのがめんどくさいって思ったら
resourcesを使うと便利です

```ex
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でも特定のルーティングは除外したいや特定のルーティングのみ使いたい場合は

```ex
# 特定のルーティングのみ指定
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

```

## ルーティングのネスト

```ex
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みたいのをつけたいとき

```ex
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とかにバージョンを持たせたいとき

```ex

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 セットアップ](http://qiita.com/su_mi/items/effc008f290d9037ece8)
[2.elixir on phoenix Mix Tasksについて](http://qiita.com/su_mi/items/d5d25a10a3d967f7b1a0)
[3.elixir on phoenix ectoを使っていろいろなSELECT文](http://qiita.com/su_mi/items/10b1c40e9c1f97f72a7f)
4.elixir on phoenix ルーティングの書き方についてまとめてみた
[5.elixir on phoenix changesetでのValidates](http://qiita.com/su_mi/items/feb97e62aa24c73b3eb0)