Last active
August 29, 2015 14:08
-
-
Save hassox/6c25e445479a6974cb3a to your computer and use it in GitHub Desktop.
struct(User, params) does not transfer values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
09:52:35.045 request_id=oOTTwz6ywSK5RsianPnN [info] POST /users | |
RAW PARAMS | |
09:52:35.069 request_id=oOTTwz6ywSK5RsianPnN [debug] Processing by CloseBy.UserController.create | |
Accept: text/html | |
Parameters: %{"user" => %{"confirm_password" => "p", "email" => "[email protected]", "password" => "p"}} | |
%{"user" => %{"confirm_password" => "p", "email" => "[email protected]", | |
"password" => "p"}} | |
THE CREATE PARAMS params["user"] | |
%{"confirm_password" => "p", "email" => "[email protected]", "password" => "p"} | |
09:52:35.192 request_id=oOTTwz6ywSK5RsianPnN [info] Sent 422 in 147ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{"confirm_password" => "p", "email" => "[email protected]", | |
"password" => "p"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule User do | |
use Ecto.Model | |
schema "users" do | |
field :email | |
field :encrypted_password | |
field :password, :virtual | |
field :confirm_password, :virtual | |
end | |
validate user, | |
email: present() | |
def set_password(user, password, password_confirmation) do | |
if (password == password_confirmation) do | |
%{user | encrypted_password: encrypt_password(password), password: nil} | |
else | |
user | |
end | |
end | |
defp encrypt_password(nil), do: nil | |
defp encrypt_password(password), do: :erlpass.hash(password, 10) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule CloseBy.UserController do | |
use CloseBy.ApplicationController | |
plug :action | |
def create(conn, params) do | |
user_params = params["user"] || %{} | |
case User.Create.from_params(user_params) do | |
{ :ok, user } -> redirect conn, CloseBy.Router.Helpers.users_path(:show, user.id) | |
{ :error, results } -> conn |> put_status(422) |> render "new", results | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule User.Create do | |
def from_params(params) do | |
password = params["password"] | |
confirmation = params["confirm_password"] | |
if password != confirmation do | |
{ :error, [{:password_confirmation, ["Does not match"]}] } | |
end | |
user = struct(User, params) | |
IO.puts("USER") | |
IO.inspect(user) | |
user = user |> User.set_password(password, confirmation) | |
case User.validate(user) do | |
[] -> | |
user = CloseBy.Repo.insert(user) | |
{ :ok, user } | |
errors -> | |
{ :error, [{:user, user}, {:errors, errors}] } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment