is there a way to return a value inside Action controller.
I have a method in my User model which returns the number of friends of a given user.
def nrOfFriends(current_user: Long): Int = {
DB.withConnection{ implicit connection =>
var nr: Int = SQL("select count(*) from friend where user_id_1=" + current_user + " or user_id_2=" + current_user).as(scalar[Int].single)
nr
}
}
In my controller, I just want to return the value from the model
def freunde() = IsAuthenticated { username => _ =>
User.findByUsername(username).map { user =>
var nr: Int = Friend.nrOfFriends(user.id.get)
Ok(""+nr)
}.getOrElse(Forbidden)
}
But in the way that is written, it will print "empty string " concatenated with the number
If I replace Ok(""+nr) with Ok(nr) I receive the following error:
"Cannot write an instance of Int to HTTP response. Try to define a Writeable[Int]"
I need for my action to return a value so that I can pass the value from the action to header.views.html inside the navbar something like that
<a href="#">@Freund.freunde Friends</a>
if you want your response to just be the value of
nr
you can simply callnr.toString
:The error you're getting makes reference to the fact that
Int
doesn't have an implicitWriteable[Int]
in scope. So play doesn't know how display anInt
in an http response.You can add make
Int
writeable by putting this in scope:Then you would be able to just say:
without error.
However, it sounds like you just want the result of
nrOfFriends
inside an unrelated template. If that's the case, you should be using anAction
at all. Instead just call your model function inside the template where you need the data.Of course you would need to pass in the user to the template as well.
You didn't post a full sample of all the code involved in what you are trying to accomplish so I think this is the best I can do for now. Perhaps try posting the base template that your
<a>
is in.An important point is that
Action
s are for production an HTTP response, and not just plain data internally to the application.