Reputation: 2063
I have a table where you input information which is then displayed, when you click the name of it I want it to go to a new page where it only displays the item clicked and not everything.
Here is my controller...
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]
}
}
My index.gsp where the recipe name is clickable which should redirect by ID to a new page where it just displays only that recipe info.
<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>
and finally my showRecipe.gsp where the recipe should be displayed by itself...but it keeps displaying all of them that i add
<g:each var="rec" in="${recipe}">
<tbody>
<tr>
<td>${rec.name}</td>
</tr>
</tbody>
</g:each>
any guidance would be awesome! thanks
Upvotes: 1
Views: 302
Reputation: 853
I could say that your first error is in your index.
It is likely that the Recipe.id that you have is retrieving all the id´s and sending them in the link. You should not use UpperCase in a property name, the compiler might think of the property as a Class. The code should be more like:
<tr>
<td><g:link action="showRecipe" id="${recipes.id}">${recipes.name}</g:link></td>
</tr>
</g:each>
Add a println(params) or log.info(params) in your show() action to print all your params and see exactly what you are receiving from your view.
Also be careful with your naming conventions. You might want to change recipe for recipeList or something, and recipes to recipeInstance or simply recipe. It will make the code more readable, and make it easier for us to help you.
As @Nitin Dhomse said you only need to access the data of a single recipe so you don´t need to do
<g:each var="rec" in="${recipe}">
in your show.gsp.
It would be more like
<table>
<tbody>
<tr>
<td>${recipe?.id}</td>
<td>${recipe?.name}</td>
....
</tbody>
</table>
Also, you should either redirect in your show() action if you dont find the recipe Instance or access your properties like $(recipe?.name) in your show.gsp, otherwise you will get nullPointer Exceptions.
Upvotes: 1