gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 577 forks source link

Windows Paths lead to error in Renderer.Auto #1927

Closed daHaimi closed 4 years ago

daHaimi commented 4 years ago

Description

The r.Auto renderer causes errors with Windows 10 and the most current version of gobuffalo.

On rendering a Resource's default method (e.g. UsersResource.List which should render the file users/index.plush.html) Leads to the Error

users/index.html: could not find template users/index.html

This happens independently on 3 Windows installations, It does not happen on 6 Linux and i MacOS installation.

Steps to Reproduce the Problem

  1. Create a default resource with CRUDL operations
  2. Run it with buffalo dev on Windows 10 (Tested with git bash, )
  3. Open the URL of one implemented operation (e.g. GET it -> List)
  4. Error

Steps to fix the problem

We tried to solve it, replacing the line

return c.Render(200, r.Auto(c, users))

with

return c.Render(200, r.HTML("users" + string(os.PathSeparator) + "index"))

This actually mitigates the error, but loses of course the context, which prevents correct rendering if variables are bound.

A simple solution would be to change render/auto.go starting from line 110:

    switch data["method"] {
    case "PUT", "POST", "DELETE":
        if err := ir.redirect(pname, w, data); err != nil {
            if er, ok := err.(ErrRedirect); ok && er.Status >= http.StatusMultipleChoices && er.Status < http.StatusBadRequest {
                return err
            }
            if data["method"] == "PUT" {
                return ir.HTML(fmt.Sprintf("%s%cedit.html", templatePrefix, os.PathSeparator)).Render(w, data)
            }
            return ir.HTML(fmt.Sprintf("%s%cnew.html", templatePrefix, os.PathSeparator)).Render(w, data)
        }
        return nil
    }
    cp, ok := data["current_path"].(string)

    defCase := func() error {
        return ir.HTML(fmt.Sprintf("%s%c%s.html", templatePrefix, os.PathSeparator, "index")).Render(w, data)
    }
    if !ok {
        return defCase()
    }

    if strings.HasSuffix(cp, "/edit/") {
        return ir.HTML(fmt.Sprintf("%s%cedit.html", templatePrefix, os.PathSeparator)).Render(w, data)
    }
    if strings.HasSuffix(cp, "/new/") {
        return ir.HTML(fmt.Sprintf("%s%cnew.html", templatePrefix, os.PathSeparator)).Render(w, data)
    }

    if !isPlural {
        return ir.HTML(fmt.Sprintf("%s%cshow.html", templatePrefix, os.PathSeparator)).Render(w, data)
    }
    return defCase()
}

Expected Behavior

It should Render the plush page

Actual Behavior

It produces an Error

Suggested Fix

Info

Output of buffalo info: buffalo info.txt

daHaimi commented 4 years ago

Additional notes:

paganotoni commented 4 years ago

Covered in v0.16.4 thanks @daHaimi