Grails 不会使用 get(params.id) 从列表中检索单个项目
Grails won't retrieve single item from list using get(params.id)
我有一个 table,您可以在其中输入信息,然后显示该信息,当您单击它的名称时,我希望它转到一个新页面,在那里它只显示单击的项目而不是所有内容。
这是我的控制器...
class RecipeController {
def index() {
def recipe = Recipes.list() //Recipes is the Grails Domain
[recipe: recipe]
}
def newRecipeForm() {
}
def createRecipe() {
def r = new Recipes(name: params.name, course: params.course, diet: params.diet)
r.save()
redirect(action:"index")
}
def deleteRecipe() {
def r = Recipes.get(params.ID)
r.delete()
redirect(action:"index")
}
def showRecipe() {
def rec = Recipes.get(params.ID)
[recipe: rec]
}
}
我的 index.gsp 其中菜谱名称是可点击的,它应该通过 ID 重定向到一个新页面,在那里它只显示该菜谱信息。
<g:each var="Recipes" in="${recipe}">
<tbody>
<tr>
<td><g:link action="showRecipe" id="${Recipes.id}">${Recipes.name}</g:link></td>
</tr>
</tbody>
</g:each>
最后是我的 showRecipe.gsp 食谱应该单独显示...但它一直显示我添加的所有食谱
<g:each var="rec" in="${recipe}">
<tbody>
<tr>
<td>${rec.name}</td>
</tr>
</tbody>
</g:each>
任何指导都很棒!谢谢
我可以说你的第一个错误是在你的索引中。
您的 Recipe.id 很可能正在检索所有 ID 并将它们发送到 link。您不应在 属性 名称中使用大写字母,编译器可能会将 属性 视为 Class。代码应该更像是:
<tr>
<td><g:link action="showRecipe" id="${recipes.id}">${recipes.name}</g:link></td>
</tr>
</g:each>
在您的 show() 操作中添加一个 println(params) 或 log.info(params) 以打印您的所有参数并准确查看您从视图中收到的内容。
还要注意您的命名约定。您可能想要将食谱更改为 recipeList 或其他东西,并将食谱更改为 recipeInstance 或简单的食谱。它将使代码更具可读性,并使我们更容易为您提供帮助。
编辑
正如@Nitin Dhomse 所说,您只需要访问单个食谱的数据,因此您不需要这样做
<g:each var="rec" in="${recipe}">
在你的 show.gsp 中。
会更像
<table>
<tbody>
<tr>
<td>${recipe?.id}</td>
<td>${recipe?.name}</td>
....
</tbody>
</table>
此外,如果您没有找到食谱实例,您应该在 show() 操作中重定向,或者在您的 show.gsp 中访问您的属性,如 $(recipe?.name),否则您将得到 nullPointer 异常.
我有一个 table,您可以在其中输入信息,然后显示该信息,当您单击它的名称时,我希望它转到一个新页面,在那里它只显示单击的项目而不是所有内容。
这是我的控制器...
class RecipeController {
def index() {
def recipe = Recipes.list() //Recipes is the Grails Domain
[recipe: recipe]
}
def newRecipeForm() {
}
def createRecipe() {
def r = new Recipes(name: params.name, course: params.course, diet: params.diet)
r.save()
redirect(action:"index")
}
def deleteRecipe() {
def r = Recipes.get(params.ID)
r.delete()
redirect(action:"index")
}
def showRecipe() {
def rec = Recipes.get(params.ID)
[recipe: rec]
}
}
我的 index.gsp 其中菜谱名称是可点击的,它应该通过 ID 重定向到一个新页面,在那里它只显示该菜谱信息。
<g:each var="Recipes" in="${recipe}">
<tbody>
<tr>
<td><g:link action="showRecipe" id="${Recipes.id}">${Recipes.name}</g:link></td>
</tr>
</tbody>
</g:each>
最后是我的 showRecipe.gsp 食谱应该单独显示...但它一直显示我添加的所有食谱
<g:each var="rec" in="${recipe}">
<tbody>
<tr>
<td>${rec.name}</td>
</tr>
</tbody>
</g:each>
任何指导都很棒!谢谢
我可以说你的第一个错误是在你的索引中。
您的 Recipe.id 很可能正在检索所有 ID 并将它们发送到 link。您不应在 属性 名称中使用大写字母,编译器可能会将 属性 视为 Class。代码应该更像是:
<tr>
<td><g:link action="showRecipe" id="${recipes.id}">${recipes.name}</g:link></td>
</tr>
</g:each>
在您的 show() 操作中添加一个 println(params) 或 log.info(params) 以打印您的所有参数并准确查看您从视图中收到的内容。
还要注意您的命名约定。您可能想要将食谱更改为 recipeList 或其他东西,并将食谱更改为 recipeInstance 或简单的食谱。它将使代码更具可读性,并使我们更容易为您提供帮助。
编辑
正如@Nitin Dhomse 所说,您只需要访问单个食谱的数据,因此您不需要这样做
<g:each var="rec" in="${recipe}">
在你的 show.gsp 中。
会更像
<table>
<tbody>
<tr>
<td>${recipe?.id}</td>
<td>${recipe?.name}</td>
....
</tbody>
</table>
此外,如果您没有找到食谱实例,您应该在 show() 操作中重定向,或者在您的 show.gsp 中访问您的属性,如 $(recipe?.name),否则您将得到 nullPointer 异常.