Pergunta

On my website I have a dropdown menu on the main navigation bar. I want all of the pages in this dropdown list to be restricted - so login is required to view them and users get redirected to the login screen if they aren't logged in. I have integrated all of the play! authenticate code into my project and looked at the example project play-authenticate-usage. In their example they have a restricted page that calls this method in the Application.java:

@Restrict(Application.USER_ROLE)
public static Result restricted() {
    final User localUser = getLocalUser(session());
    return ok(restricted.render(localUser));
}

This method returns the rendered page to be viewed. I tried duplicating this method so I can return the restricted page I want to:

@Restrict(Application.USER_ROLE)
public static Result restrictedCreate() {
    final User localUser = getLocalUser(session());
    return ok(journeyCreator.render(localUser));
}

I added this new method the routes file:

GET     /restricted                         controllers.Application.restrictedCreate()

And modified by dropdown code so that it would call my new method:

<li><a href="@routes.Application.restrictedCreate()"><i class="icon-plus-sign"></i> @Messages("journeys.dropdown.option1")</a></li>

At this stage I am getting a compilation error: error: method render in class journeyCreator cannot be applied to given types; so I check the page I am trying to render journeyCreator.scala.html and add the localUser: models.User = null arguement. My journeyCreator.scala.html now looks this this:

@(localUser: models.User = null, listJourneys: List[Journey], journeyForm: Form[Journey])

@import helper._

@main("Journey Creator", "journeys") {
        ......
    }
}

However doing this causes all sorts of errors: error: method render in class journeyCreator cannot be applied to given types; in other methods concerned with journeyCreator.scala.html. Any help is appreciated.

Foi útil?

Solução

You declared a params of the view (which is function), but didn't pass them, so it's causing the problem.

Although in Scala functions (similar to PHP) you can set default values for params, Java has problems with it, so you need to pass something in the place, it could be just... null

public static Result restrictedCreate() {
    final User localUser = getLocalUser(session());
    return ok(journeyCreator.render(localUser, null, null));
}

Later use @if(localUser!=null){ ... } condition in the view to make sure you have what you need.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top