The setup:
Using Play! framework v 2.0.4
The controller:
def javascriptRoutes = Action { implicit request =>
Ok(
Routes.javascriptRouter("jsRoutes")(
routes.javascript.Admin.approve
)
).as("text/javascript")
}
def approve(user: List[String]) = SecureAction('admin) { implicit ctx =>
Logger.debug("Admin.approve: " + user.foldLeft("")(_ + "::" + _))
user map { u =>
User.approve(u)
}
Ok(Json.toJson(user))
}
The view:
function get_selected() {
return $.makeArray($(".user-selector").map(function (ind, user){
if(user.checked) return user.name;
}));
}
$("#button-approve").click(function(){
jsRoutes.controllers.Admin.approve(get_selected()).ajax({
success: function(data, status) {
console.log("Users activated: " + data)
for(i = 0; i < data.length; i++) {
id = "#" + data[i];
$(id + " > td > i.approved").removeClass("icon-flag").addClass("icon-check");
}
$(":checked").attr("checked", false);
}
});
});
The routes:
PUT /admin/users controllers.Admin.approve(user: List[String])
GET /admin/jsRoutes controllers.Admin.javascriptRoutes
I also used the code mentioned in this question to allow binding of List[String]
as a parameter.
The problem
The parameters are passed in a request reported like this:
PUT /admin/users?user=506b5d70e4b00eb6adcb26a7%2C506b6271e4b00eb6adcb26a8
The encoded %2C
character being a comma. The controller interprets it as a single string because the debug line from the code above looks like this:
[debug] application - Admin.approve: ::506b5d70e4b00eb6adcb26a7,506b6271e4b00eb6adcb26a8
(using the default List.toString
was misleading that's why I used the foldLeft
trick).
So
How to pass the list of checkbox selected users to the controller so it is interpretted as a list of strings, and not list of single string?
Ok. The problem was in the old implementation of
QueryBinders
that were missing the JavaScript part. The proper version is:Now the query looks like this:
And finally everything works.
Final note:
In the Play! framework 2.1+ it should work without adding the code in the project sources. Actually the code is borrowed as-is from
Play20/framework/src/play/src/main/scala/play/api/mvc/Binders.scala