Phoenix - assign @changeset not available in eex template

Below is my code

  def create(conn, %{"create_video" => video_params}) do

    admin_id  = get_session(conn, :admin_id)

    with {:ok, name} <- parse_name(video_params['name']),
         {:ok, video_file} <- parse_file(video_params['video'])
         
    do 
      video_fields = %{name: name, video_location: video_file, admin_id: admin_id}

      case Videos.create_video(video_fields) do
        {:ok, video} ->
          conn
          |> put_flash(:info, "Video created successfully.")
          |> redirect(to: Routes.video_path(conn, :show, video))

        {:error, %Ecto.Changeset{} = changeset} ->
          render(conn, "new.html", changeset: changeset, error: nil)
      end
    else
      
      {:error, error}->
        render(conn, "new.html", error: error)
    end
  end

My template file


                        <%= form_for @changeset, @action, [as: :create_video, id: "product-form", class: "mt-5 mb-5 login-input", method: :post, multipart: :true], fn f -> %>

                          <%= if @changeset.action do %>
                            <div class="alert alert-danger">
                              <p>Oops, something went wrong! Please check the errors below.</p>
                            </div>
                          <%= end %>
                          <%= if @error do %>
                            <div class="alert alert-danger">
                              <p><%= @error %> </p>
                            </div>     
                          <% end %>

                          <div class="form-group">
                            <%= label f, :name %>
                            <%= text_input f, :name, name: "create_video[name]", 
                              placeholder: "Name", class: "form-control" %>
                            <%= error_tag f, :name %>
                          </div>
                          <div class="form-group">
                            <%= label f, :video_location %>
                            <%= file_input f,  :video_file, name: "create_video[video]", 
                              class: "form-control", placeholder: "Upload Video
                            " %>
                            <%= error_tag f, :video_location %>
                          </div>

                          <div>
                            <%= submit "Save", class: "btn btn-dark mb-2" %>
                          </div>
                        <% end %>

I can understand changeset assign is missing in

render(conn, "new.html", error: error)

So I did

render(conn, "new.html", error: error, changeset: nil)

render(conn, "new.html", error: error, changeset.action: nil)

Both are not working. How to fix this? Your help is greatly appreciated. Thanks

You should probably create a fake changeset from video_params…

You have 2 parse validations that have nothing to do with create_video, but with upload validation.

Other than that, waffle_ecto can validate directly in the changeset.

1 Like

Any example code would help me more?

Yes exactly.

You should have an example in your context…

probably like this…

%Video{}
    |> Video.changeset(video_params)