GeekyTrash
GeekyTrash

Reputation: 51

How to get a passed "id" param through a link from a gsp file to another gsp file in grails

Suppose I have a gsp file with a link with is working with the tag "Read more. This links opens another gsp file name "blog" and if you look at the url bar, you can see my current link is "MyWebApp/post/blog/(current i value). So how do I get the (current i value) wich is actually an integer, from the new blog.gsp file?

Upvotes: 0

Views: 1543

Answers (3)

user800014
user800014

Reputation:

When you create links or submit forms, your information is stored in the params Map. This map is accessible by your controller and with that you can do whatever you need like passing the data to your view, or perform query's and then pass the result to the view.

The simplest way of understand the flow controller > view > controller > another view is to use grails command generate-all and check the basic crud for your domain class. See how the show and edit works.

Gregg's answer is probably what you're looking for. If it not works, maybe you're passing an invalid id. You can check if the post exists before showing the content using <g:if>, for example:

<g:if test="${post}">
  id: ${post.id}
</g:if>
<g:else>
<p>This blog post don't exists.</p>
</g:else>

Upvotes: 0

rimero
rimero

Reputation: 2383

UPDATE

You can probably do the following

<g:link action="blog" controller="post" params="['id': '${i}']">
   Read more
</g:link>

It depends on what i is via url mappings, but I guess it would be ${params.i}

Upvotes: 0

Gregg
Gregg

Reputation: 35864

class PostController {

   def list() {
     // this action calls the page that has the "Read more" link
     def posts = Post.list()
     [posts: posts]
   }

   def blog() {
     // this action is triggered by the "Read more" link and
     // renders your blog post where you want the current ID
     def post = Post.get(params.id)
     [post: post]
   }
}

blog.gsp

<html>
....
${post.id}
....
</html>

Upvotes: 1

Related Questions