Hi!
I have a problem after modifying the standard registration flow from Phauxth. I have generated the auth with Phauxth and modified it to add first_name, last_name and password confirmation and changed the route scope to /admin. The registration works fine if all fields are provided, but if there is a validation error, it fails with the following message:
ArgumentError at POST /admin/users
cannot convert nil to param
The code looks like this:
Form:
<%= form_for @changeset, user_path(@conn, :create), fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= text_input f, :first_name %>
<%= error_tag f, :first_name %>
</div>
<div class="form-group">
<%= text_input f, :last_name %>
<%= error_tag f, :last_name %>
</div>
<div class="form-group">
<%= email_input f, :email %>
<%= error_tag f, :email %>
</div>
<div class="form-group">
<%= password_input f, :password %>
<%= error_tag f, :password %>
</div>
<div class="form-group">
<%= password_input f, :password_confirmation %>
<%= error_tag f, :password_confirmation %>
</div>
<%= submit "Register" %>
<% end %>
user_controller.ex
def create(conn, %{"user" => %{"email" => email} = user_params}) do
key = Phauxth.Token.sign(conn, %{"email" => email})
case Accounts.create_user(user_params) do
{:ok, user} ->
Log.info(%Log{user: user.id, message: "user created"})
Accounts.Message.confirm_request(email, key)
success(conn, "User created successfully", session_path(conn, :new))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
accounts.ex
def create_user(attrs) do
%User{}
|> User.create_changeset(attrs)
|> Repo.insert()
end
user.ex
def create_changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:first_name, :last_name, :email, :password])
|> validate_required([:first_name, :last_name, :email, :password])
|> unique_email
|> validate_confirmation(:password)
|> validate_password(:password)
|> put_pass_hash
end
router.ex
scope "/admin", MyApp do
resources "/users", Admin.UserController
end
If I inspect the changeset it looks like this:
#Ecto.Changeset<
action: :insert,
changes: %{
email: "email@gmail.com",
last_name: "last name",
password: "password"
},
errors: [
password_confirmation: {"does not match confirmation",
[validation: :confirmation]},
first_name: {"can't be blank", [validation: :required]}
],
data: #MyApp.Accounts.User<>,
valid?: false
Appreciate any help with this.