When opening the url
/users/{id}/foo
I will display the view with a call to
html.foo(userWithId)
In the controller method foo I want to make sure that id is the same as the loggedin user’s id and if not should redirect the user to
/users/{theLoggedInUsersId}/foo
using
html.foo(loggedInUser)
works fine but this is not a redirect, so the url in the browser still is
/users/{id}/foo
I want to really redirect so that the url is showing the correct id. I’m not sure how to do this. Using “Action” like this:
Action(foo(loggedInUsersId))
will get an error for using recursion without supplying a return type. Adding the return type play.template.Html to the controller method foo, will get a compiler error since Action returns a ScalaAction and not Html.
How should I do this. Should I even do it, am I stuck thinking about it in the wrong way?
Edit
The Redirect(…) works fine. But is it possible to do without it?
In pseudo-Scala:
def list(id: Long) = {
if (some_criteria)
html.list(user_with_id_equal_to_id)
else if(another_criteria)
list(another_user_id)
else
Action(Application.login)
}
The list(another_user_id) won’t work since it’s a recursive call and I have to supply a return type on the list method. Adding the return type play.templates.Html won’t work since that is not the return type of Action
Do you see what I’m getting at? If I instead of using a call to list uses html.list(user_with_another_id) then it won’t be a redirect and the url in the browser will still be /users/id/foo instead of /user/another_id/foo.
There are two ways to go about doing this. The first is to use the Redirect return type.
The way you want is to use the Action like you suggested.
This should work fine. Play actually resolves this call as URL and calls a redirect. So the first example I showed you is pretty much the same thing. The Action works better because it prevents you from having to change code if you change the url. We may need to see more of your code to see what’s going on.
Here is a more clear example.
Reference: http://scala.playframework.org/documentation/scala-0.9.1/controllers#Returntypeinference