1
votes

I have been following the book Programming Phoenix. I started with the 6th chapter Generators and Relationships and I am not able to follow the book due to the following reasons

The book says to run this command

    mix phoenix.gen.html Video videos user_id:references:users \
url:string title:string description:text

I was having problems running this command but changing this to

mix phoenix.gen.html Video Videos user_id :references: users url:string title:string description:text

worked(for that step)

But Instead of creating the required files in the web/controllers, web/templates and web/views directory it creating everything in the lib directory (as given in the screenshot 1 below)

enter image description here Screenshot 1

Then when I run mix ecto.migrate command after following the book on page 95 I get this videos_path/3 function undefined error in /lib/rumbl_web/controllers/video_controller.ex line 22 as follows on screenshot 2

enter image description here Screenshot 2

line 22 is like this

 redirect(to: videos_path(conn, :show, videos))

inside video_controller.ex

and the complete file is as follows:

    defmodule RumblWeb.VideosController do
  use Rumbl.Web, :controller

  alias Rumbl.Video
  alias Rumbl.Video.Videos

  def index(conn, _params) do
    user_id = Video.list_user_id()
    render(conn, "index.html", user_id: user_id)
  end

  def new(conn, _params) do
    changeset = Video.change_videos(%Videos{})
    render(conn, "new.html", changeset: changeset)
  end

  def create(conn, %{"videos" => videos_params}) do
    case Video.create_videos(videos_params) do
      {:ok, videos} ->
        conn
        |> put_flash(:info, "Videos created successfully.")
        |> redirect(to: videos_path(conn, :show, videos))
      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

  def show(conn, %{"id" => id}) do
    videos = Video.get_videos!(id)
    render(conn, "show.html", videos: videos)
  end

  def edit(conn, %{"id" => id}) do
    videos = Video.get_videos!(id)
    changeset = Video.change_videos(videos)
    render(conn, "edit.html", videos: videos, changeset: changeset)
  end

  def update(conn, %{"id" => id, "videos" => videos_params}) do
    videos = Video.get_videos!(id)

    case Video.update_videos(videos, videos_params) do
      {:ok, videos} ->
        conn
        |> put_flash(:info, "Videos updated successfully.")
        |> redirect(to: videos_path(conn, :show, videos))
      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "edit.html", videos: videos, changeset: changeset)
    end
  end

  def delete(conn, %{"id" => id}) do
    videos = Video.get_videos!(id)
    {:ok, _videos} = Video.delete_videos(videos)

    conn
    |> put_flash(:info, "Videos deleted successfully.")
    |> redirect(to: videos_path(conn, :index))
  end
end

I am unable to get it as I am new to phoenix and elixir. Can someone please help me out here as to where I went wrong and what should I do to solve it,please?

1
mix phoenix.gen.html is a legacy, the generator is phx.gen.html for more than a year already. - Aleksei Matiushkin
@AlekseiMatiushkin thanks for the reply, is there a command to revert the changes made by mix phoenix.gen.html, or do I have to manually remove the files created by this command. - abhishek ranjan
I honestly have no idea. - Aleksei Matiushkin
no issues, I'll try to find out. - abhishek ranjan

1 Answers

2
votes

Your problem is using functions which is on different versions of Phoenix. This comes from reading old version book of Programming Phoenix (now the latest is 1.4, which update for Phoenix 1.4). You should read the latest book to learn the latest Phoenix.

From the release notes 1.3

1.3.0 uses the phx. prefix on all generators. The old generators are still around though to give the community and learning resources time to catch up. ?They will be removed on 1.4.0

Here is why:

According to structure of project at screenshot 1, you start the project by using

mix phx.new rumbl

But after that, the book guide to old function

mix phoenix.gen.html

This causes all problems.

For the first problem: the \ means the code is on the same line

mix phoenix.gen.html Video videos user_id:references:users \
url:string title:string description:text