简体   繁体   English

带有“--skip-model”标志的“生成资源”命令导致资源错误

[英]“Generate resource” command with “--skip-model” flag results in faulty resource

Description描述

Just started using Buffalo, trying out all the beautiful features:)刚开始使用 Buffalo,尝试了所有漂亮的功能:)

I'm having an issue however with the "generate resource" command in combination with the "--skip-model" flag.但是,我遇到了“生成资源”命令与“--skip-model”标志相结合的问题。 When this flag is used, all the generated functions ("List", "Show", "Create", "Update" and "Destroy") are created fully in lowercase.使用此标志时,所有生成的函数(“List”、“Show”、“Create”、“Update”和“Destroy”)都完全以小写形式创建。 The struct however that is also generated refers to "buffalo.Resource" and contains these functions with the first letter in uppercase, resulting in a resource that doesn't work.然而,同样生成的结构引用“buffalo.Resource”并包含这些函数,其中第一个字母为大写,导致资源不起作用。

Steps to Reproduce the Problem重现问题的步骤

  1. Use generate resource command with "--skip-model" flag: buffalo g r todo --skip-model .使用带有“--skip-model”标志的生成资源命令: buffalo g r todo --skip-model
  2. Run the application using: buffalo dev .使用: buffalo dev运行应用程序。
  3. Navigate to "http://127.0.0.1:3000/todoes";导航到“http://127.0.0.1:3000/todoes”; verify that you get an error saying "runtime error: invalid memory address or nil pointer dereference".验证您是否收到错误消息“运行时错误:无效的 memory 地址或零指针取消引用”。
  4. Verify in the generated file that "todoes.go" contains the generated functions ("List", "Show", "Create", "Update" and "Destroy") fully in lowercase, while the generated struct called "TodoesResource" refers to "buffalo.Resource" and contains these functions with the first letter in uppercase.在生成的文件中验证“todoes.go”是否包含完全小写的生成函数(“List”、“Show”、“Create”、“Update”和“Destroy”),而生成的名为“TodoesResource”的结构是指“buffalo.Resource”并包含这些函数,其中第一个字母为大写。

Expected Behavior预期行为

I expected the generated functions to have the first letter in uppercase, matching the names in "buffalo.Resource" and resulting in the response "Todo#list" when navigating to "http://127.0.0.1:3000/todoes" (after starting the application).我希望生成的函数的第一个字母为大写,与“buffalo.Resource”中的名称匹配,并在导航到“http://127.0.0.1:3000/todoes”时产生响应“Todo#list”(之后启动应用程序)。 This is the case when you don't use the "--skip-model" flag, so I'm not sure why this would behave differently when you do use this flag.当你不使用“--skip-model”标志时就是这种情况,所以我不确定为什么当你使用这个标志时它的行为会有所不同。

Actual Behavior实际行为

The generated functions ("List", "Show", "Create", "Update" and "Destroy") are fully in lowercase, while the generated struct called "TodoesResource" refers to "buffalo.Resource" and contains these functions with the first letter in uppercase.生成的函数(“List”、“Show”、“Create”、“Update”和“Destroy”)完全小写,而名为“TodoesResource”的生成结构指的是“buffalo.Resource”并包含这些函数第一个字母大写。 This results in the error "runtime error: invalid memory address or nil pointer dereference" when navigating to "http://127.0.0.1:3000/todoes" (after starting the application).当导航到“http://127.0.0.1:3000/todoes”(启动应用程序后)时,这会导致错误“运行时错误:无效的 memory 地址或零指针取消引用”。

Suggested solution(s)建议的解决方案

I'm not able to create a pull request (as I get the error "Permission to gobuffalo/buffalo.git denied" when trying to publish a branch), but I think there are two possible solutions to this issue:我无法创建拉取请求(因为我在尝试发布分支时收到错误“gobuffalo/buffalo.git 的权限被拒绝”),但我认为这个问题有两种可能的解决方案:

Preferred solution首选解决方案

Modifying the file "genny/resource/templates/standard/action/resource-name.go.tmpl" to change the code below:修改文件“genny/resource/templates/standard/action/resource-name.go.tmpl”修改如下代码:

// {{$a.String}} default implementation.
func (v {{$.opts.Name.Resource}}Resource) {{$a.String}}(c buffalo.Context) error {
  return c.Render(http.StatusOK, r.String("{{$.opts.Model.Proper}}#{{$a.String}}"))
}

And change this to:并将其更改为:

// {{$a.Pascalize}} default implementation.
func (v {{$.opts.Name.Resource}}Resource) {{$a.Pascalize}}(c buffalo.Context) error {
  return c.Render(http.StatusOK, r.String("{{$.opts.Model.Proper}}#{{$a.Pascalize}}"))
}

Alternative solution替代解决方案

Modifying the file "genny/resource/actions.go" to change the code below:修改文件“genny/resource/actions.go”以更改以下代码:

func actions(opts *Options) []name.Ident {
    actions := []name.Ident{
        name.New("list"),
        name.New("show"),
        name.New("create"),
        name.New("update"),
        name.New("destroy"),
    }
    if opts.App.AsWeb {
        actions = append(actions, name.New("new"), name.New("edit"))
    }
    return actions
}

And change this to:并将其更改为:

func actions(opts *Options) []name.Ident {
    actions := []name.Ident{
        name.New("List"),
        name.New("Show"),
        name.New("Create"),
        name.New("Update"),
        name.New("Destroy"),
    }
    if opts.App.AsWeb {
        actions = append(actions, name.New("New"), name.New("Edit"))
    }
    return actions
}

This was a bug and is currently being fixed.这是一个错误,目前正在修复中。 Also see: https://github.com/gobuffalo/buffalo/issues/2023 .另请参阅: https://github.com/gobuffalo/buffalo/issues/2023

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM