Please help with UndefinedFunctionError

Hello! I learning phoenix by book "Programmin Phoenix Productive > Reliable > Fast.
And I can’t fix error “function Rumbl.Repo.get/2 is undefined or private”. I don’t understand what’s wrong, maybe you can help me?

I think problem in this module, but don’t khow why

defmodule Rumbl.UserController do
	use Rumbl.Web, :controller
	
    alias Rumbl.Repo
	
	def index(conn, _params) do
		users = Repo.all(Rumbl.User)
		render conn, "index.html", users: users
	end
	
	def show(conn, %{"id" => id}) do
		user = Repo.get(Rumbl.User, id)
		render conn, "show.html", user: user
	end
end

Screenshots https://yadi.sk/i/wy6VxHZLfIUH0A
https://yadi.sk/i/MZ062xVGfl7bVQ

How did you defined Rumbl.Repo? Because it seems to cause problems there.

Hi,

Welcome to the forum

What version of programming phoenix are you using? 1.4 or 1.3?

It seems You are following the old version of programming Phoenix… (for 1.2). There is an updated version now.

https://pragprog.com/book/phoenix14/programming-phoenix-1-4

in repo.ex file
and alias Rumbl.Repo dont’ was include to example code, I try add this after readint stackoverflow

defmodule Rumbl.Repo do
	@moduledoc """
	In memory repository.
	"""
	
	def all(Rumbl.User) do
		[%Rumbl.User{id: "1", name: "José", username: "josevalim", password: "elixir"},
		%Rumbl.User{id: "2", name: "Bruce", username: "redrapids", password: "7langs"},
		%Rumbl.User{id: "3", name: "Chris", username: "chrismccord", password: "phx"}]
	end
	def all(_module), do: []
	def get_by(module, params) do
		Enum.find all(module), fn map ->
			Enum.all?(params, fn {key, val} -> Map.get(map, key) == val end)
		end
	end
end

Hello, thanks, Version 1.2.5.

Hello for everyone. I check version now - it’s 1.2.5. And I check source code from book, but it different than in my books example. My book of 2016 year, Code from source

defmodule RumblWeb.UserController do
  use RumblWeb, :controller

  alias Rumbl.Accounts

  def index(conn, _params) do
    users = Accounts.list_users()
    render(conn, "index.html", users: users)
  end

  def show(conn, %{"id" => id}) do
    user = Accounts.get_user(id)
    render(conn, "show.html", user: user)
  end
end

Oh, shame shame shame. I forget or delete get function from repo.ex. Thanks all for
your help)

defmodule Rumbl.Repo do
	@moduledoc """
	In memory repository.
	"""
	
	def all(Rumbl.User) do
		[%Rumbl.User{id: "1", name: "José", username: "josevalim", password: "elixir"},
		%Rumbl.User{id: "2", name: "Bruce", username: "redrapids", password: "7langs"},
		%Rumbl.User{id: "3", name: "Chris", username: "chrismccord", password: "phx"}]
	end
	def all(_module), do: []
	def get(module, id) do
		Enum.find all(module), fn map -> map.id == id end
	end
	def get_by(module, params) do
		Enum.find all(module), fn map ->
			Enum.all?(params, fn {key, val} -> Map.get(map, key) == val end)
		end
	end
en
2 Likes

You are welcome, great that you solved the problem