我有以下控制器:
defmodule Campaigns.CampaignController do
use Campaigns.Web, :controller
alias Campaigns.Campaign
alias Campaigns.ApiParams
plug :ensure_account_id
def index(conn, %{"account_id" => account_id}) do
query = from c in Campaign, where: [account_id: ^account_id]
render(conn, "index.json", campaigns: Repo.all(query))
end
def show(conn, %{"id" => id, "account_id" => account_id}) do
query = from c in Campaign, where: [id: ^id, account_id: ^account_id]
render(conn, "show.json", campaign: Repo.one(query))
end
defp ensure_account_id(conn, _) do
cs = ApiParams.changeset(%ApiParams{}, conn.params)
case cs do
%{:params => %{"account_id" => account_id}, :valid? => true} ->
conn
_ ->
conn
|> put_status(400)
|> render(conn, "400.json", "account_id is missing")
end
end
end
我想确保在对此控制器中的每个端点发出的请求中存在account_id
。我得到的控制台中的错误是
[error] #PID<0.403.0> running Campaigns.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/campaigns/1
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Phoenix.Controller.render/4
但是我的ErrorView.ex文件中有一个render("400.json"
方法。
这是我的ErrorView.ex
文件
defmodule Campaigns.ErrorView do
use Campaigns.Web, :view
def render("404.html", _assigns) do
"Page not found"
end
def render("500.html", _assigns) do
"Internal server error"
end
def render("400.json", %{"message" => error}) do
%{error: error}
end
# In case no render clause matches or no
# template is found, let's render it as 500
def template_not_found(_template, assigns) do
render "500.html", assigns
end
end
答案 0 :(得分:1)
defp ensure_account_id(conn, _) do
cs = ApiParams.changeset(%ApiParams{}, conn.params)
case cs do
%{:params => %{"account_id" => account_id}, :valid? => true} ->
conn
_ ->
conn
|> put_status(400)
|> render("400.json", message: "account_id is missing") # remove the `conn` here
end
端