grailsInvas0r
grailsInvas0r

Reputation: 655

createLink not working in (mail) templates?

I have a Controller action and sending a mail in it with something like:

mailService.sendMail {
    ...
    g.render(template: "mailtemplate")
}

in this template file is called _mailtemplate.gsp I use

<a href="<g:createLink controller="servicecontroller" action="confirm"/>">linktext</a>

But the output is http://action ... that's it! I would expect to have http://www.example.com/action. If I use the same createLink tag in a gsp which is not a template it's working (by the way, email is working fine and all the other stuff in this template is rendered well).

Have you any suggestions on that?

Upvotes: 2

Views: 1569

Answers (2)

Alexandre Bourlier
Alexandre Bourlier

Reputation: 4118

QUOTE: I must specify a serverURL in config file, but I want it dynamically

You can probably do as such:

config.groovy:

environments {
    development {
        grails.serverURL = "http://localhost:8080"
    }
    production {
        grails.serverURL = "http://www.mywebsite.com"
    }
}

Then in your service sending the email:

    import org.codehaus.groovy.grails.commons.ConfigurationHolder

    def baseURL = ConfigurationHolder.config.grails.serverURL

    mailService.sendMail {
    ...
    g.render(template: "mailtemplate", model:['baseURL':baseURL])
    }

And at last in your link:

 <a href="<g:createLink controller="servicecontroller" action="confirm" base="${baseURL}"/>">linktext</a>

I hope this helps

Upvotes: 1

Igor Artamonov
Igor Artamonov

Reputation: 35961

Probably you need absolute link:

<a href="<g:createLink controller="servicecontroller" action="confirm" absolute="true"/>">linktext</a>

Btw, you can also use ${} syntax there, like:

<a href="${g.createLink(controller: "servicecontroller", action: "confirm", absolute: true)}">linktext</a>

Upvotes: 3

Related Questions