Question

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!

Was it helpful?

Solution

My steps in this case would be as follows:

  1. 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>
    
     }
    
  2. run command play clean

  3. Then run 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:

  • @(company: Company) could not be resolved to the model Company.
  • The last compilation had a @(company: String)

Good luck!

OTHER TIPS

I don't know if this will fix your problem or not but it's worth a try. Try removing changing:

@{company.name}

to:

@company.name

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top