Phoenix: After generation ** (CompileError) web/controllers/role_controller.ex:20: function role_path/3 undefined

Created on 15 Sep 2015  路  3Comments  路  Source: phoenixframework/phoenix

After simple secvence of commands:

mix phoenix.new hexp --module Hexp --app hexp
cd hexp

mix test
...
Finished in 0.2 seconds (0.2s on load, 0.00s on tests)
4 tests, 0 failures

mix phoenix.gen.json Role roles name:string

  • creating web/controllers/role_controller.ex
  • creating web/views/role_view.ex
  • creating test/controllers/role_controller_test.exs
  • creating web/views/changeset_view.ex
  • creating priv/repo/migrations/20150915060932_create_role.exs
  • creating web/models/role.ex
  • creating test/models/role_test.exs

mix test
Compiled web/views/changeset_view.ex
Compiled web/views/role_view.ex
Compiled web/models/role.ex

== Compilation error on file web/controllers/role_controller.ex ==
** (CompileError) web/controllers/role_controller.ex:20: function role_path/3 undefined
(stdlib) lists.erl:1337: :lists.foreach/2
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8

Where is role_path/3 Defined? I need to add by hand ?

grep -R role_path .
./test/controllers/role_controller_test.exs: conn = get conn, role_path(conn, :index)
./test/controllers/role_controller_test.exs: conn = get conn, role_path(conn, :show, role)
./test/controllers/role_controller_test.exs: get conn, role_path(conn, :show, -1)
./test/controllers/role_controller_test.exs: conn = post conn, role_path(conn, :create), role: @valid_attrs
./test/controllers/role_controller_test.exs: conn = post conn, role_path(conn, :create), role: @invalid_attrs
./test/controllers/role_controller_test.exs: conn = put conn, role_path(conn, :update, role), role: @valid_attrs
./test/controllers/role_controller_test.exs: conn = put conn, role_path(conn, :update, role), role: @invalid_attrs
./test/controllers/role_controller_test.exs: conn = delete conn, role_path(conn, :delete, role)
./web/controllers/role_controller.ex: |> put_resp_header("location", role_path(conn, :show, role))

Most helpful comment

After running mix phoenix.gen.json you would have had the following output:

Add the resource to your api scope in web/router.ex:

    resources "/roles", RoleController, except: [:new, :edit]

Remeber to update your repository by running migrations:

    $ mix ecto.migrate

You didn't add the routes so role_path/3 is undefined.

All 3 comments

After running mix phoenix.gen.json you would have had the following output:

Add the resource to your api scope in web/router.ex:

    resources "/roles", RoleController, except: [:new, :edit]

Remeber to update your repository by running migrations:

    $ mix ecto.migrate

You didn't add the routes so role_path/3 is undefined.

Thank you @sevenseacat!

Nice :D
Good to generate code that is compiled with no action.

Just one idea:

defmodule Hexp.RoleController do
use Hexp.Web, :controller, router_path: : role_path
# Define here def role_path and call dinamicaly Router function
# Code is compile
# If ruter function is not found, we can show good message
end

Just thinking ...
Thanks!

Was this page helpful?
0 / 5 - 0 ratings