exmanga

exmanga

Getting "cannot convert nil to param" error from Phauxth registration

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.

Most Liked

exmanga

exmanga

Anyone?

Or maybe I should just dump this and go with Ueberauth?

Last Post!

exmanga

exmanga

hm, adding something like this in the controller doesn’t help

plug :scrub_params, "user" when action in [:create]

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement