remoteLink can't find controller - grails

I can call the closure of a controller in a gsp view using the following link:
<g:link url="${request.contextPath}/data/${params.name}/myController/myClosure">
Text Goes Here
</g:link>
However I would like to use remoteLink because I don't want to render anything, just perform a task. Am I correct to want to use remoteLink?
Unfortunately remoteLink does not have a url attribute. So I use controller and action:
<g:remoteLink controller="myController" action="myClosure">
Text Goes Here
</g:remoteLink>
Which unsuccessfully uses the url "/myApp/MyController/MyClosure", which is different from the url that works.
So my questions are,
How did my controller get that longer url? It does not appear in my URLMappings. What controls this?
Is there anyway I can use remoteLink to access that url?
Thank you!

The URL mapping must be named:
static mappings = {
name <mapping name>: <url pattern> {
// …
}
}
Then you can do:
<g:remoteLink mapping="<mapping name>" controller="myController" action="myClosure">
Text Goes Here
</g:remoteLink>

Related

Use Grails Form tag with Controller in Subfolder

I'd like to send an email using a controller in a subfolder. I can't seem to find the syntax to accomplish this.
The default would be something like this...
<g:form controller="email" action="sendEmail">
// form stuff
</g:form>
But my email controller, called Email.groovy, is in controllers/portal/Utils. Maybe this is outside normal convention?
Change your controller class name to EmailController.groovy

Using one views gsp in another views gsp

So my question is, hopefully, very simple.
I have two different domains with corresponding controllers/view folders etc.
I want to call the second _form.gsp from the first _form.gsp (within a g:each as there will be multiple things to display) and pass in the relevant data for the second _form.gsp to render from, how do I do that?
I know I can use g:render template = "myotherForm" but I don't know how to point it to another view folder or how to pass in the details...
Thanks in advance and let me know if more information is required...
You can Refer this
Grails uses the convention of placing underscore before the name of a view to identify it as a template.
Example grails-app/views/book/_bookTemplate.gsp:
<div class="book" id="${book?.id}">
<div>Title: ${book?.title}</div>
<div>Author: ${book?.author?.name}</div>
</div>
Then use render tag
<g:render template="bookTemplate" model="[book: myBook]" />
You're right, <g:render is the right tool. By default, for template="myOtherTempalte" it looks for tempalte in current directory, but you could pass full path, if it's from another controller. Like template="/forms/myOtherTemplate". And use model="" to pass parameters, same as inside controller:
<g:each in="${things}" var="x">
<g:render template="/forms/myOtherTemplate" model="${thing: x}"/>
</g:each>

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?
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>
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}
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>

Grails URL mapping cause error on GSP

I have a site that have URL similar to this:
/mysite/admin/controller/action/id
/mysite/search/controller/action/id
/mysite/user/controller/action/id
I have my URL mapping like this
"/$prefix/$controller/$action?/$id?"{
constraints {}
}
I am able to get to the controller correctly.
But on the GSP side
<g:link controller="controller">abc</g:link> ==> abc
Notice how I lose the prefix between mysite and the controller.
You can use named url mappings and then pass the prefix as part of the params:
URLMappings:
name prefix: "/$prefix/$controller/$action?/$id?"{
constraints {}
}
GSP:
<g:link mapping="prefix" params="[prefix:$prefix, controller:...]">abc</g:link>
To use sortableColumn, just put all of the URLMapping parameters in the params property:
<g:sortableColumn property="col" title="title" params="[ prefix: 'prefix', controller:'controller', action:'action']" />
It works when you hit the URL in browser, because prefix is available in URL. It does not work when you use link tag to create url, because grails does not have information about which prefix should be used for this controller. You will need to provide the value for prefix to link tag.
Try this
<g:link controller="controller" params="[prefix:'admin']">abc</g:link>
in-short - You have to pass those dynamic variables as params if you want link re-writing to consider them. Read more docs here

how can i redirect from default controller action to my generated controller action?

in my list page of scaffolding i have put a link in span.. on click of that link i want it to take me my own created controller action..
i tried giving <g:link controller="mycontroller",action="myaction">mylink</g:link>
that din work for me then i tried
<g:link action="redirectingAction">mylink</g:link>
but now i am not able to redirect from this redirectingAction in default controller which i edited there
what can i do to come out of this problem?
Your code <g:link controller="mycontroller",action="myaction">mylink</g:link> is wrong. Remove the comma before the action parameter and it should work:
<g:link controller="mycontroller" action="myaction">mylink</g:link>

Resources