Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Auto Render not correctly redirecting POST/PUT on resource if using i…
…t in group #1299 (#1332)
  • Loading branch information
markbates committed Sep 27, 2018
1 parent e9e7068 commit 939bbac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion render/auto.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"path"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -150,7 +151,12 @@ func (ir htmlAutoRenderer) redirect(name inflect.Name, w io.Writer, data Data) e
rt := reflect.TypeOf(fi)
zero := reflect.Zero(rt)
if fi != zero.Interface() {
url := fmt.Sprintf("/%s/%v", name.URL(), f.Interface())
url := fmt.Sprint(data["current_path"])
id := fmt.Sprint(f.Interface())
url = strings.TrimSuffix(url, "/")
if !strings.HasSuffix(url, id) {
url = path.Join(url, id)
}

code := 302
if i, ok := data["status"].(int); ok {
Expand Down
18 changes: 18 additions & 0 deletions render/auto_test.go
Expand Up @@ -223,6 +223,24 @@ func Test_Auto_HTML_Create_Redirect_Error(t *testing.T) {
r.NoError(err)
}

func Test_Auto_HTML_Create_Nested_Redirect(t *testing.T) {
r := require.New(t)

app := buffalo.New(buffalo.Options{})
admin := app.Group("/admin")
admin.POST("/cars", func(c buffalo.Context) error {
return c.Render(201, render.Auto(c, Car{
ID: 1,
Name: "Honda",
}))
})

w := httptest.New(app)
res := w.HTML("/admin/cars").Post(nil)
r.Equal("/admin/cars/1", res.Location())
r.Equal(302, res.Code)
}

func Test_Auto_HTML_Edit(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit 939bbac

Please sign in to comment.