Current section

Files

Jump to
torch priv templates common phx.gen.html controller.ex
Raw

priv/templates/common/phx.gen.html/controller.ex

defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>Controller do
use <%= inspect context.web_module %>, :controller
alias <%= inspect context.module %>
alias <%= inspect schema.module %>
plug(:put_root_layout, {<%= inspect context.web_module %>.LayoutView, "torch.html"})
plug(:put_layout, false)
def index(conn, params) do
case <%= inspect context.alias %>.paginate_<%= schema.plural %>(params) do
{:ok, assigns} ->
render(conn, "index.html", assigns)
error ->
conn
|> put_flash(:error, "There was an error rendering <%= schema.human_plural %>. #{inspect(error)}")
|> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :index))
end
end
def new(conn, _params) do
changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(%<%= inspect schema.alias %>{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{<%= inspect schema.singular %> => <%= schema.singular %>_params}) do
case <%= inspect context.alias %>.create_<%= schema.singular %>(<%= schema.singular %>_params) do
{:ok, <%= schema.singular %>} ->
conn
|> put_flash(:info, "<%= schema.human_singular %> created successfully.")
|> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
<%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
render(conn, "show.html", <%= schema.singular %>: <%= schema.singular %>)
end
def edit(conn, %{"id" => id}) do
<%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>)
render(conn, "edit.html", <%= schema.singular %>: <%= schema.singular %>, changeset: changeset)
end
def update(conn, %{"id" => id, <%= inspect schema.singular %> => <%= schema.singular %>_params}) do
<%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
case <%= inspect context.alias %>.update_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params) do
{:ok, <%= schema.singular %>} ->
conn
|> put_flash(:info, "<%= schema.human_singular %> updated successfully.")
|> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", <%= schema.singular %>: <%= schema.singular %>, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
<%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
{:ok, _<%= schema.singular %>} = <%= inspect context.alias %>.delete_<%= schema.singular %>(<%= schema.singular %>)
conn
|> put_flash(:info, "<%= schema.human_singular %> deleted successfully.")
|> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :index))
end
end