mirror of
https://github.com/onkelbeh/cheatsheets.git
synced 2025-06-15 14:47:53 +02:00
Update
This commit is contained in:
parent
404ffc8c0c
commit
89b67ed4ac
@ -6,14 +6,20 @@ category: Elixir
|
||||
## Creating
|
||||
|
||||
```
|
||||
$ mix ecto.gen.migration add_posts_table
|
||||
creating priv/repo/migrations/20160602085927_add_posts_table.exs
|
||||
$ mix ecto.gen.migration update_posts_table
|
||||
creating priv/repo/migrations/20160602085927_update_posts_table.exs
|
||||
...
|
||||
|
||||
$ mix ecto.migrate
|
||||
$ mix ecto.rollback
|
||||
```
|
||||
|
||||
### Creating models
|
||||
|
||||
```
|
||||
$ mix phoenix.gen.model Message messages user_id:integer content:text
|
||||
# creates models and tests
|
||||
```
|
||||
|
||||
## Tables
|
||||
|
||||
|
73
phoenix-routing.md
Normal file
73
phoenix-routing.md
Normal file
@ -0,0 +1,73 @@
|
||||
---
|
||||
title: "Phoenix: Routing"
|
||||
category: Elixir
|
||||
---
|
||||
|
||||
```sh
|
||||
mix phoenix.routes
|
||||
```
|
||||
|
||||
## Single routes
|
||||
|
||||
```elixir
|
||||
get "/", PageController, :index
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
```elixir
|
||||
resources "/users", UserController
|
||||
resources "/users", UserController, only: [:index, :show]
|
||||
resources "/users", UserController, except: [:delete]
|
||||
```
|
||||
|
||||
| Method | Path | Helper |
|
||||
| ---- | ---- | ---- |
|
||||
| GET | `/users` | `user_path(:index)` |
|
||||
| GET | `/users/new` | `user_path(:new)` |
|
||||
| GET | `/users/:id` | `user_path(:show, user)` |
|
||||
| GET | `/users/:id/edit` | `user_path(:edit, user)` |
|
||||
| POST | `/users` | `user_path(:create, user)` |
|
||||
| PATCH/PUT | `/users/:id` | `user_path(:update, user)` |
|
||||
| DELETE | `/users/:id` | `user_path(:delete, user)` |
|
||||
|
||||
## Path helpers
|
||||
|
||||
```elixir
|
||||
user_path(Endpoint, :index) #=> /users
|
||||
user_path(Endpoint, :show, 17) #=> /users/17
|
||||
user_path(Endpoint, :show, %User{id: 17}) #=> /users/17
|
||||
user_path(Endpoint, :show, 17, admin: true) #=> /users/17?admin=true
|
||||
|
||||
user_path(Endpoint, :index) #=> "http://localhost:4000/users"
|
||||
```
|
||||
|
||||
```elixir
|
||||
MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index)
|
||||
```
|
||||
|
||||
## Nested resources
|
||||
|
||||
```elixir
|
||||
resources "/users", UserController do
|
||||
resources "/posts", PostController
|
||||
end
|
||||
|
||||
user_post_path(:index, 17) #=> /users/17/posts
|
||||
user_post_path(:show, 17, 12) #=> /users/17/posts/12
|
||||
```
|
||||
|
||||
## Scoped routes
|
||||
|
||||
```elixir
|
||||
scope "/admin" do
|
||||
pipe_through :browser
|
||||
resources "/reviews", MyApp.Admin.ReviewController
|
||||
end
|
||||
# reviews_path() -> /admin/reviews
|
||||
```
|
||||
|
||||
```elixir
|
||||
scope "/admin", as: :admin do: ... end
|
||||
# admin_reviews_path() -> /admin/reviews
|
||||
```
|
50
phoenix.md
50
phoenix.md
@ -16,24 +16,46 @@ web/
|
||||
static/
|
||||
```
|
||||
|
||||
## Migrations
|
||||
## Plug.Conn
|
||||
|
||||
```
|
||||
$ mix ecto.gen.migration add_posts_table
|
||||
creating priv/repo/migrations/20160602085927_add_posts_table.exs
|
||||
...
|
||||
### Request
|
||||
|
||||
$ mix ecto.migrate
|
||||
```elixir
|
||||
conn.host #=> "example.com"
|
||||
conn.method #=> "GET"
|
||||
conn.path_info #=> ["posts", "1"]
|
||||
conn.request_path #=> "/posts/1"
|
||||
conn.query_string #=> "utm_source=twitter"
|
||||
conn.port #=> 80
|
||||
conn.scheme #=> :http
|
||||
conn.peer #=> {{127, 0, 0, 1}, 12345}
|
||||
conn.remote_ip #=> {151, 236, 219, 228}
|
||||
conn.req_headers #=> [{"content-type", "text/plain"}]
|
||||
```
|
||||
|
||||
### [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration)
|
||||
### Response
|
||||
|
||||
```elixir
|
||||
conn.resp_body #=> "..."
|
||||
conn.resp_charset #=> "utf-8"
|
||||
conn.resp_cookies #=> ...
|
||||
conn.resp_headers #=> ...
|
||||
conn.status #=> ...
|
||||
```
|
||||
|
||||
### Misc
|
||||
|
||||
```elixir
|
||||
conn.assigns # storage of crap
|
||||
conn.owner # process
|
||||
conn.halted # if pipeline was halted
|
||||
conn.secret_key_base # ...
|
||||
conn.state # :unset, :set, :file, :sent, :chunked
|
||||
```
|
||||
|
||||
### Session
|
||||
|
||||
```
|
||||
create index(:posts, [:slug], concurrently: true)
|
||||
create table(:documents) do
|
||||
add :body, :string
|
||||
add :deletion_key, :string
|
||||
|
||||
timestamps
|
||||
end
|
||||
conn = put_session(conn, :message, "new stuff we just set in the session")
|
||||
get_session(conn, :message)
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user