Skip to content

Instantly share code, notes, and snippets.

View lasseebert's full-sized avatar

Lasse Skindstad Ebert lasseebert

View GitHub Profile
def update(conn, %{"id" => id, "user" => user_params}) do
user = Repo.get!(User, id)
changeset = User.changeset(user, user_params)
case Repo.update(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: user_path(conn, :show, user))
{:error, changeset} ->
$ mix phoenix.new --no-html --no-brunch my_app
* creating my_app/config/config.exs
* creating my_app/config/dev.exs
* creating my_app/config/prod.exs
# ... more files here
* creating my_app/priv/static/js/phoenix.js
* creating my_app/priv/static/images/phoenix.png
* creating my_app/priv/static/favicon.ico
Fetch and install dependencies? [Yn]
defmodule MyApp.UserController.InviteTest do
use MyApp.ConnCase
test "inviting a user responds with the new user" do
conn =
build_conn
|> post("/users", email: "alice@example.com")
body = conn |> response(201) |> Poison.decode!
defmodule MyApp.UserController.InviteTest do
use MyApp.ConnCase
test "inviting a user responds with the new user" do
conn =
build_conn
|> post("/users", email: "alice@example.com")
body = conn |> response(201) |> Poison.decode!
$ mix test
...
1) test inviting a user responds with the new user (MyApp.UserController.InviteTest)
test/controllers/user_controller/invite_test.exs:4
** (RuntimeError) expected response with status 201, got: 404, with body:
{"errors":{"detail":"Page not found"}}
stacktrace:
(phoenix) lib/phoenix/test/conn_test.ex:362: Phoenix.ConnTest.response/2
test/controllers/user_controller/invite_test.exs:9: (test)
defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/", MyApp do
pipe_through :api
$ mix test
Compiling 3 files (.ex)
...
1) test inviting a user responds with the new user (MyApp.UserController.InviteTest)
test/controllers/user_controller/invite_test.exs:4
** (UndefinedFunctionError) function MyApp.UserController.init/1 is undefined (module MyApp.UserController is not available)
stacktrace:
MyApp.UserController.init(:create)
(my_app) web/router.ex:1: anonymous fn/1 in MyApp.Router.match_route/4
defmodule MyApp.UserController do
use MyApp.Web, :controller
def create(conn, _params) do
conn
|> resp(201, "{}")
end
end
body = conn |> response(201) |> Poison.decode!
assert body["id"] > 0
assert body["email"] == "alice@example.com"
$ mix test
Compiling 1 file (.ex)
Generated my_app app
...
1) test inviting a user responds with the new user (MyApp.UserController.InviteTest)
test/controllers/user_controller/invite_test.exs:4
Assertion with == failed
code: body["email"] == "alice@example.com"
lhs: nil