Reputation: 6005
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!
Upvotes: 0
Views: 997
Reputation: 5951
My steps in this case would be as follows:
Change the scala template, we hve to tell the scala templates the our Company
belongs to the model class: (but also change to @company.name
as suggested by Jordan.
@(company: models.Company)
@main("Welcome to Play 2.0") {
<h1>@company.name</h1>
}
run command play clean
play debug ~run
By executing play debug ~run
you 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 render
object wants a string could be the result of:
model
Company.Good luck!
Upvotes: 1
Reputation: 2737
I don't know if this will fix your problem or not but it's worth a try. Try removing changing:
to:
Upvotes: 0