I working on an http api app creating the controllers/router info manually and I am getting this error:
**function PhxReactList7Web.BlogsController.init/1 is undefined (module PhxReactList7Web.BlogsController is not available)**
here are the files in play:
blogs_controller.ex
defmodule PhxReactList7Web.BlogController do
use PhxReactList7Web, :controller
alias PhxReactList7.Blogs
alias PhxReactList7.Blogs.Blog
def index(conn, _params) do
blogs = Repo.all(Blogs)
end
end
router.ex
defmodule PhxReactList7Web.Router do
use PhxReactList7Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", PhxReactList7Web do
pipe_through :browser
get "/", PageController, :index
get "/blogs", BlogsController, :index # show all blog posts
end
if Mix.env() in [:dev, :test] do
import Phoenix.LiveDashboard.Router
scope "/" do
pipe_through :browser
live_dashboard "/dashboard", metrics: PhxReactList7Web.Telemetry
end
end
end
after searching the web for an answer and coming up empty, I posted the question. the lab I am working from is using Phoenix v1.3 and Elixir 1.6. The lab uses the database model feature and the folder structure is somewhat different than the one used now.
Also don’t forget that Phoenix actions need to return a conn your current function returns an :ok (the default value when there is none). So you might need to do something like:
def index(conn, _params) do
blogs = Repo.all(Blog)
render(conn, "index.json", blogs: blogs)
end
More info on rendering JSON you can find here: Rendering JSON
@ joaoevangelista
thanks for the tip. I’m working with the labs from a 1917 article that uses phx 1.3 which is different to the 1.5.7 version that I’m using. I guess I could download the 1.3 version but I want to learn to the current version which is what is being used in my new job starting 2 weeks from now.
I will explore your suggested changes to see if they work. the problem I’m also having is that every time I make a change to the code I end up getting a ton of other error messages like:
protocol Ecto.Queryable not implemented for PhxReactList7.Blogs of type Atom, the given module does not provide a schema. This protocol is implemented for the following type(s): Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple
so I’m still catching my breath…
thanks again… this really helps… I’m slow, but getting there…
using you code change I now get the following error message:
function PhxReactList7Web.BlogsView.render/2 is undefined (module PhxReactList7Web.BlogsView is not available)
I’m going to start the project from scratch and go from there, this project is now so broken the error messages get more confusing by the second. no… nano second…
anywho: Obrigado
it’s because now it returns correctly but there is no view module. You can check about it here. What you can do to easy your learning is to create a new Phoenix project and scaffold all the pieces. Then you can change code all around to see how things are setup.
The guides section of the documentation also helps a lot with understanding what is needed for Phoenix
Versions in the 1.0 major version does not break anything, just deprecate.