Append to query-string with the same key - grails

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

Related

AdWords PLACEMENT_PERFORMANCE_REPORT not pulling URLs

This should be extremely simple but for some reason it doesn't seem to work. I'm trying to pull the URLs of display placements using the DISPLAY_PERFORMANCE_REPORT but instead of URLs it's just returning "--".
The code I'm using is:
var report = AdWordsApp.report(
"SELECT CampaignName, Clicks, FinalAppUrls, FinalUrls " +
"FROM PLACEMENT_PERFORMANCE_REPORT " +
"WHERE Clicks > 0 " +
"DURING LAST_30_DAYS");
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
var url = row["FinalUrls"];
Logger.log(url);
}
I've tried logging the CampaignName and clicks and they're working as expected, so can't understand what the issue is here. The only thing I can think of is that in the reference guide it says:
List of final URLs of the main object of this row. UrlList elements
are returned in JSON list format
I'm not entirely sure what JSON list format is, but when I log the typeof url it says it's a string, so thought it shouldn't be an issue.
The FinalAppUrls and FinalUrls list the target URLs that you set on the individual managed placements.
If you're interested in the URL (domain, rather) of the placement itself, you'll have to request either the Criteria or the DisplayName field in your report——they both contain the domain of the placement.

How to concatenate API request URL safely

Let's imagine I have the following parts of a URL:
val url_start = "http://example.com"
val url_part_1 = "&fields[...]&" //This part of url can be in the middle of url or in the end
val url_part_2 = "&include..."
And then I try to concatenate the resulting URL like this:
val complete_url = url_start + url_part_2 + url_part_1
In this case I'd get http://example.com&include...&fields[...]& (don't consider syntax here), which is one & symbol between URL parts which means that concatenation was successful, BUT if I use different concat sequence in a different request like this:
val complete_url = url_start + url_part_1 + url_part_2
I'd get http://example.com&fields[...]&&include..., to be specific && in this case. Is there a way to ensure that concatenation is safer?
To keep you code clean use an array or object to keep your params and doin't keep "?" or "&" as part of urlStart or params. Add these at the end. e.g.
var urlStart = "http://example.com"
var params=[]
params.push ('a=1')
params.push ('b=2')
params.push ('c=3', 'd=4')
url = urlStart + '?' + params.join('&')
console.log (url) // http://example.com?a=1&b=2&c=3&d=4
First, you should note that it is invalid to have query parameters just after domain name; it should be something like http://example.com/?include...&fields[...] (note the /? part, you can replace it with / to make it a path parameter, but it's not likely that the router of the website supports parameters like this). Refer, for example, to this article: https://www.talisman.org/~erlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/ to know more about what URLs can be valid.
For the simple abstract approach, you can use Kotlin's joinToString():
val query_part = arrayOf(
"fields[...]",
"include..."
).joinToString("&")
val whole_url = "http://example.com/?" + query_part
print(whole_url) // http://example.com/?fields[...]&include...
This approach is abstract because you can use joinToString() not only for URLs, but for whatever strings you want. This also means that if there will be an & symbol in one of the input strings itself, it will become two parameters in the output string. This is not a problem when you, as a programmer, know what strings will be joined, but if these strings are provided by user, it can become a problem.
For URL-aware approach, you can use URIBuilder from Apache HttpComponents library, but you'll need to import this library first.

How do I add a query parameter to a URL?

What's the best practice for adding a query parameter to a URL in Tritium (Moovweb SDK)? Looking for something that works where you don't know if the URL has a "?" and other query parameters already.
Here's a short snippet of Tritium that should help you out in your Moovweb project. Just replace the "query_param=true" bit with the query parameter you want to add.
It selects the href of every a tag, then looks for any existing query parameters (by looking for a "?" in the href). If there are some existing, it just appends the new query parameter. If there are no existing query parameters on the href, it uses the ? to add one to the URL.
$q = "query_param=true"
$("//a[#href]") {
%s = fetch("./#href")
match(%s) {
with(/\?/) {
attribute("href", %s + "&" + $q)
}
else() {
attribute("href", %s + "?" + $q)
}
}
log(%s)
}
(You could also turn that into a function if you wanted!)
I think there is going to be a new URL scope soon so you'll be able to do things like this much more easily!

Building a URL with ActionLinks from within a Controller in MVC

I'm building a basic flashcard application - a Set contains Cards and other Sets. When navigating to something like Set1 > Set2 > Set3, I want each of the parent "Sets" to link to their respective details page. I'm passing the Details viewModel a string called "breadcrumbs", which contains something like this:
var stringBuilder = "";
var parent = model.ParentSet;
while (parent != null)
{
stringBuilder += "<li><a href=\"#Url.Action('Detail', 'Set', new {SetId = \" + parentSetId + \"}, null)'>" + parent.Name + "</a> <span class='divider'>/</span></li>";
parent = parent.ParentSet;
}
model.Breadcrumbs =
"<li>Home <span class='divider'>/</span></li>" +
stringBuilder + "<li class='active'>New Set</li>";
model.Name = "New Set";
The URLs being generated are literal though - containing "#Url.Action".
Does anyone know how I can get this to work, or if there's a better way to do it?
Just evaluate it:
"<li><a href='" + Url.Action('Index', 'Home') + "'>..."
I think an important question here is, "What does your view look like?"
If your view is using that model.Breadcrumbs field like this:
...
<div id="breadcrumbs">
#model.Breadcrumbs
</div>
...
The #Url.Action call in the string you've built will never be executed. The Razor engine operates on the code it finds in the view file, not on fields within the model.
You need to do one of two things:
Rather put "#Url.Action(...)" into the string you're constructing, figure out what the Url is on the server side using something like Url.Link() and insert that directly.
(and this is better) Pass those parents to the view as part of the view model and loop over them in the view, constructing the tags as you go. Normally you don't want to put html into a model. The model's there to hold data, not presentation code.

Removing "undefined" in pre populated url form link (from Modified Google Expense Report script)

I've modified the standard google drive expense report system to use it as a competency and experience matrix where the manager can approve/reject a form.
The problem i'm encountering is when a form is rejected by the manager it sends a link back to the employee with a link to a form pre-populated by the url.
However if the employee didn't enter a value in the original form the url populates it as "undefined".
Question, can I pre-populate a form via url removing or replacing the "undefined" values?
Perhaps the code will explain better than I can.
+ "<P><b>" + 'Please re-submit your form <a href=' + USER_FORM_URL +
'&entry_2=' + encodeURIComponent(row.forename) + '&entry_3=' +
encodeURIComponent(row.surname) + '>here</a></b>'
Any help would be much appreciated, struggled to find a solution "out there".
Tom
image link http://s11.postimage.org/dyd1olkoz/url_stack.jpg as i'm a new poster.
Looks like the getObjects() function in the script may skip adding a value to the row object, later used in sendReportToManager(), if the value is empty in the expense report spreadsheet. So, later trying to append something like row.bananas (or something empty in sheet and thus not defined on object) will result in 'undefined' being appended to the string instead. If you look at your querystring for the form with the 'undefined' values, you should see 'undefined' in the url's querystring.
You can avoid this by wrapping your encodeURIComponent(row.XXXX) calls for potentially empty columns to be something like this: row.XXXX ? encodeURIComponent(row.XXXX) : ""
So, replace the likes of:
+ "<P><b>" + 'Please re-submit your form <a href=' + USER_FORM_URL +
'&entry_2=' + encodeURIComponent(row.forename) + '&entry_3=' +
with
+ "<P><b>" + 'Please re-submit your form <a href=' + USER_FORM_URL +
'&entry_2=' + (row.forename ? encodeURIComponent(row.forename) : "") + '&entry_3=' +

Resources