I'm trying to create a link with a dynamic link like:
<g:link action="${nextDashboardUriMap.nextAction}" params="${["$nextDashboardUriMap.queryStringId": "$entityId" ]}">
${entityName}
</g:link>
where nextDashboardUriMap.queryStringId contains xyz and entityId contains 12.
I was expecting url of the link to be http://website.com/controller/action?xyz=18 but <g:link/> consistently gives me http://website.com/controller/action?xyz.
I have tried replacing entityId with a string literal.
You don't need to use GStrings here, you can simply say
<g:link action="${nextDashboardUriMap.nextAction}"
params="[(nextDashboardUriMap.queryStringId):entityId]">
It seems that it may be this bug: GRAILS-9774 - the value is lost if key in params map is of type GString. Converting the key to String should resolve your problem:
<g:link action="${nextDashboardUriMap.nextAction}"
params="${[("$nextDashboardUriMap.queryStringId".toString()): "$entityId" ]}">
(...)
how about
<%
Map paramsMap = [:]
paramsMap[nextDashboardUriMap.queryStringId] = entityId
%>
<g:link action="${nextDashboardUriMap.nextAction}" params="${paramsMap}" >${entityName}
</g:link>
Related
I'm adding a custom error
trip.errors.rejectValue('driver','Driver already on a trip at this time.')
When I output the error in a view....
<g:hasErrors bean="${trip}" field="driver">
<g:renderErrors bean="${trip}" field="driver" as="list" />
</g:hasErrors>
....a fully qualified field path gets appended to it.
"Driver already on a trip at this time. org.heidelberg.Trip.driver"
How can I get rid of the org.heidleberg.Trip.driver part that is applied?
Your method calling is incorrect.
Try this -
trip.errors.rejectValue('driver',null,'Driver already on a trip at this time.')
it should work fine.
The correct method signature of rejectValue method is
void rejectValue(String field, String errorCode, String defaultMessage
Cheers!!
I am currently trying to figure out, how to modify the parameter being integrated into the URL Mapping I am using.
static mappings =
{
"/$controller/$action?/$id?/(.$format)?"
{
constraints {
// apply constraints here
}
}
name test1: "/.../$title/..."{
controller = "study"
action = "st_show"
}
name test2: "/.../$title/..."{
controller = "search"
action = "se_show"
}
The parameter $title is pretty much a dataset, which is pulled from a database and which will get transmitted in the following format [ this is a title ]. So there are square brackets in front and behind the string and words are seperated through blanks.
If I am creating a link through g:link now with the params nested in, it gets put into the url as it is pulled from the database. What I am attempting is to create SEO-URLs, which will present a certain title of a publication devided by hyphens instead of url-encoded "%20".
Until now, I was able to generate dynamic urls looking like this:
http://localhost:8080/projectname/show/%5BAllgemeine%20Bevölkerungs[...]/782/...PARAMS...
Furthermore I already implemented it through JQuery, though it should be static and users should be able to copy the link to open up the page themselves - that wouldn't be possible when changing the url client-side while loading up the page.
Is there a way to define a function with something like replaceAll.(' ', '-'), which can be invoked onto the parameter in the mapping to replace blanks with hyphens and f.e. square brackets with an empty character?
That's pretty much, what I wasn't able to come by through the documentation.
Thank you already in advance for your help!
I managed to solve my problem by creating a service with a function containing a regex and executing this function onto the parameter title in my g:link, which I firstly converted to a string, which gets passed to the function.
<g:link controller="study" action="st_show" params="[data: data, ... title: ConversionService.convert(fieldValue(bean: path).toString(), ... data: data)]"></g:link>
And the function in the ConversionService
public static String convert(String title){
title = title.replaceAll("\\s", "-").replaceAll("[^0-9a-zA-Z\\-]", "");
return title;
}
I am trying to add some parts to a query-string with the same key.
I have a url like http://server.com/search/result?query=hello with some controls that should add a collaborator to the query-string.
This is done with this:
<g:link action="result" params="${params + ['collaborator': collaborator.id]}">${collaborator.name}</g:link>
Which returns: http://server.com/search/?query=hello&collaborator=<id>.
This is all good, but its possible to choose more collaborators. How do I append another &collaborator=<id> to the query?
I have tried various methods like ${params + [params.collaborator + collaborator.id]}, but this only puts them in one string.
Try this:
gsp:
<g:link action="result" params="${params + ['collaborator': params.list('collaborator') + [collaborator.id]]}">${collaborator.name}</g:link>
action:
List collaboratorList = params.list('collaborator')
//Your operations on this list
Is it possible to add multiple values in the id attribute of glink tag?
I want to get a url like: /place/view/home/7? (/< controller >/< action >/< name >/< id >?params)
is it possible with glink tag?
If I understand correctly what you're looking for is the params option,
<g:link controller="cname" action="act" id="1" params="[sort: 'title']" />
I use the following solution in Grails v2.4.3:
In UrlMappings.groovy, I added:
static mappings = {
...
name dualId: "/$controller/$action/$id/$id2?(.$format)?" {}
/* Usage:
* <g:link mapping="dualId" action="myMethod" params='[id2: "${id2Value}"]'>
* Accept id AND id2
* </g:link>
* Generates following link in the UI:
* Accept id AND id2
* SPECIAL NOTE: params='[id2: "${id2Value}"]' works.
* using: params="[id2: '${id2Value}']" DOES NOT WORK!
*/
...
}
For your requirements, "/place/view/home/7? (/< controller >/< action >/< name >/< id >?params)", you could use:
static mappings = {
...
name placeViewHome: "/$controller/$action/$name/$id?" {}
/* Usage:
* <g:link mapping="placeViewHome" action="myAction" params='[name: "${nameValue}", q: "str"]'>
* Accept name AND id
* </g:link>
* Should generate the following link in the UI:
* Accept name AND id
* I didn't try it, but you should be able to add in other params and I would expect them to appear after the question mark as usual.
*/
...
}
See Grails documentation, 8.4.11 Named URL Mappings, for more detail...
Note: In the g:link, the order of the quotes mattered, double quotes inside single quotes worked, but not the other way around.
In my erb file I have this:
"name": "<%= o =*('a'..'z'),*('A'..'Z')
string = o.sample(10).join %>",
That assigns a random generated string as the value to the name element as the key.
It is generating the random string fine but in RubyMine IDE it says
"expected <% or <%= or ;"
at the place of the second "*" in the code? Why is that and how to fix it?
Also more importantly, How can I can a prefix to this generated string? so for example let's say this generates string of abcDef But I need it to be MyPrefix_abcDef
The IDE is correct, there is an error. You need to replace
o =*('a'..'z'),*('A'..'Z')
with
o =[*'a'..'z'] + [*'A'..'Z']