I think I have misunderstood something about the Play 2 framework.
In my Application Controller I fetch a Company object from the DB
and I would like to make some operations on it in my view.
companyView.scala.html:
@(company: Company)
@main("Welcome to Play 2.0") {
<h1>@{company.name}</h1>
}
Application Controller:
package controllers;
import models.Company;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result company(String rest) {
Company company =
Company.find.where().ilike("restfulIdentifier.identifier", rest).findUnique();
return ok(companyView.render(company));
}
}
But return ok(companyView.render(company)); results in compilation error since companyView.render wants a string.
If I look at the forms sample application:
/**
* Handle the form submission.
*/
public static Result submit() {
Form<Contact> filledForm = contactForm.bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest(form.render(filledForm));
} else {
Contact created = filledForm.get();
return ok(summary.render(created));
}
}
There is no problem with rendering an object. I guess that the solution is fairly simple and
that I have missed some crucial part of the documentation. Please explain this to me!
My steps in this case would be as follows:
Change the scala template, we hve to tell the scala templates the our
Companybelongs to the model class: (but also change to@company.nameas suggested by Jordan.run command
play cleanplay debug ~runBy executing
play debug ~runyou will trigger to compile the the play application on each SAVE of one of your project files.NOTE: The Play templates are basically functions. These functions needs to be compiled and everything used in these functions needs to be declared before use. Just as in regular Java development.
The fact that the your
renderobject wants a string could be the result of:modelCompany.Good luck!