私はginライブラリを使用して単純なcrud webappを作成しています.paramをチェックするルート設定がありid
、そのadd
aadmin-employee-add.html
をレンダリングする必要がある場合は、存在する場合はidを持つ従業員を返しますgetsadmin-employee-add.html
のエラーメッセージのテンプレートをレンダリングしている場合404 not found
その中に漏れました。ここにスナップショットがあります
admin-employee-add.html
{{template "pageStart.html" .}}
<form class="form-horizontal admin-employee">
<div class="row form-group">
<label for="employeeNumber" class="col-lg-2 control-label text-right">Employee #</label>
<div class="col-lg-4">
<span>new</span>
</div>
<label for="status" class="col-lg-2 control-label text-right">Status</label>
<div class="col-lg-4">
<span>new</span>
</div>
</div>
<div class="row form-group">
<label for="firstName" class="col-lg-2 control-label text-right">Name</label>
<div class="col-lg-2">
<input type="text" id="firstName" name="firstName" class="form-control">
</div>
<div class="col-lg-2">
<input type="text" id="lastName" name="lastName" class="form-control">
</div>
</div>
<div class="row form-group">
<label for="startDate" class="col-lg-2 control-label text-right">Start Date</label>
<div class="col-lg-2">
<input type="date" id="startDate" name="startDate" class="form-control">
</div>
<label for="pto" class="control-label col-lg-2 col-lg-offset-2 text-right">PTO</label>
<div class="col-lg-3">
<input type="number" id="pto" name="pto" class="form-control">
</div>
<div class="col-lg-1">
days
</div>
</div>
<div class="row form-group">
<label for="position" class="col-lg-2 control-label text-right">Position</label>
<div class="col-lg-2">
<select name="position" id="position" class="form-control">
<option value="CEO">CEO</option>
<option value="CTO">CTO</option>
<option value="COO">COO</option>
<option value="WorkerBee">Worker Bee</option>
</select>
</div>
</div>
<div class="col-lg-3 col-lg-offset-8">
<button type="submit" class="btn btn-lg admin-primary">Create</button>
</div>
</form>
エラーを作成しているルート
r.GET("/employee/:id/", func(c *gin.Context) {
id := c.Param("id")
if id == "add" {
c.HTML(http.StatusOK, "admin-employee-add.html", nil)
}
employee, ok := employees[id]
if !ok {
c.String(http.StatusNotFound, "404 not found", nil)
} else {
c.HTML(http.StatusOK, "admin-employee-edit.html", map[string]interface{}{
"Employee": employee,
})
}
})
ginがリダイレクトしようとしているためにエラーが発生しているようです/add
->/add/
しかし、私はすでに/add/
ブラウザーでルートを使用しています。
ginのデバッグログ
[GIN-debug] GET /login --> main.registerRoutes.func2 (3 handlers)
[GIN-debug] GET /employee/:id/ --> main.registerRoutes.func3 (3 handlers)
[GIN-debug] GET /employee/:id/vacation --> main.registerRoutes.func4 (3 handlers)
[GIN-debug] GET /admin/ --> main.registerRoutes.func5 (4 handlers)
[GIN-debug] Listening and serving HTTP on :3000
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with
404
[GIN] 2016/05/01 - 14:12:13 | 404 | 1.101426ms | 127.0.0.1 | GET /employee/add/
ルートを変更してみまし/:id
たが、エラーが表示されました。
redirecting request 301: /employee/add/ --> /employee/add
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with
404
return
注: このエラーは、.txtの末尾にa を追加することで簡単に解決できますif id == "add"
。しかし、このパターンにより、コードがドライに見えなくなります。これはもっとhttprouter
問題のようです。