5
votes

My application works and api json requests and for regular html. My router.ex

defmodule MyApp.Router do
  use MyApp.Web, :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 "/api", MyApp do
    pipe_through :api # Use the default browser stack

    scope "/v1", V1, as: :v1 do
      resources "/users", UserController, except: [:new, :edit, :index]
    end
  end

  scope "/", MyApp do
    pipe_through :browser # Use the default browser stack
    get "/confirm/:token",          UserController, :confirm, as: :user_confirm
  end

end

my web/controllers/v1/user_controller.ex

defmodule MyApp.V1.UserController do
  use MyApp.Web, :controller

  def create(conn, %{"user" => user_params}) do
   ...
          conn
          |> put_status(:created)
          |> put_resp_header("location", v1_user_path(conn, :show, user))
          |> render("sign_up.json", user: Map.put(user, :session, result[:session]))
   ...
  end

and my web/controllers/user_controller.rb

defmodule MyApp.UserController do
  use MyApp.Web, :controller

  alias MyApp.User

  def confirm(conn, %{"token" => token}) do
...
            render(conn, "confirmed.html")
...
  end

end

my web/views/v1/user_view.ex

defmodule MyApp.V1.UserView do
  use MyApp.Web, :view
...
end

and my web/views/user_view.ex

defmodule MyApp.UserView do
  use MyApp.Web, :view

end

Everything works fine until I added a route and a controller for html. Now, when I make a request for api json, I get an error

Request: POST /api/v1/users
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function MyApp.V1.UserView.render/2 is undefined (module MyApp.V1.UserView is not available)

But if I delete web/vews/user_view.ex, then this query works without errors. How can you correct this error?

1
I see these errors sometimes when there has been a compile issues and I have not restarted the app. Try double crtl-c out of the phoenix server. Also try running mix clean, the restart the phoenix server. The only other thing I can think of is an alias issue. You could temporarily renaming one of the UserView modules to see if that helps.Steve Pallen
@StevePallen Yes, renaming MyApp.UserView for example in UserViewHtml solves the problem. But I was hoping that there is another solutionMarsel.V
Perhaps there is an issue with the compile/loading. You can also try adding Code.ensure_loaded/1 to your html module.Steve Pallen
It seems mix clean really helped! It's strange that it did not help for the first time, but now I ran it again, and it works. Thank you!Marsel.V
Added an answer for quicker access for other readers.Steve Pallen

1 Answers

5
votes

These types of errors can usually be resolved by running mix clean. You may also see this type of error during Live code reload in dev. It case, try restarting the Phoenix.Server, and if that does not help, run mix clean