开发者

grails controllers simple question

I have this controller:

def participated = {

    def user = User.get(1)
    def temp = ConferenceUser.findAllByUser(user)
    def prizes = Prizes.list()

    [temp: temp, prizes: prizes]

    //redirect(action: "participated", params: params)
}

And this gsp view:

<g:each in="${temp}">

  <li>Date: ${it.dateParticipated}, <br>Role: ${it.role}, <br>Acceptence: ${it.accepted}, <br>
  <g:link controller="conference" action="participated" params="[temp: it.conf开发者_JAVA技巧erence.id]">
     Conference: </g:link>${it.conference},<br>
Status: ${it.status}</li><br>

   <g:each in="${prizes.findAllByConferenceUser(temp.id)}"> ########
     PrizeName: ${it.name}
</g:each>
  <br>
</g:each>

What i want to do is, after printing all the conferenceUser objects i need, i need to print all the prizes won with that conferenceUser id. Where the ## is, there is my problem. I dont know how to do that in the controller, because 'temp' gives me all conferenceUser filtered by User. But i also need all prizes won filtered by conferenceUSer id. So i passed in the controller temp (which represents all the conferences with the user id = 1) And i passed all the prizes won in order to filter them in the second 'each' tag. But this way is not working, because, as i is now, temp.id represents multiple ids (as shown in the errors). Any help please? How can i get the id that is being used at that moment in the first each tag?

Error 500: Error evaluating expression [prizes.findAllByConferenceUser(temp.id)] on line [44]: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.findAllByConferenceUser() is applicable for argument types: (java.util.ArrayList) values: [[50, 51, 52, 53, 54, 55, 57]]
Servlet: grails
URI: /CM/grails/conferenceUser/participated.dispatch
Exception Message: No signature of method: java.util.ArrayList.findAllByConferenceUser() is applicable for argument types: (java.util.ArrayList) values: [[50, 51, 52, 53, 54, 55, 57]]
Caused by: Error evaluating expression [prizes.findAllByConferenceUser(temp.id)] on line [44]: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.findAllByConferenceUser() is applicable for argument types: (java.util.ArrayList) values: [[50, 51, 52, 53, 54, 55, 57]]
    Code Snippet:


ok, so you are looping thru conferences, and then trying to do a find on prize for each conference. It seems like you want a one to many relationship between Conference and Prize domain objects. If you set that up, you will not have any trouble reaching the Prizes from a given Conference.

A few notes:

First, never load things via 'domainObject.find...()' from gsps. Its an extremely bad practice that is only going to get you in trouble.

Second, all database operations should really be behind services. Doing it in a controller and not in a service is not as bad as loading data from the gsp, but its not a good idea. Having services is a good idea because they wrap all your data operations in transactions, and they allow your controllers to just be controllers.

You need to understand that Grails is a convention driven framework. It is based on MVC, which is a pattern where View concerns are kept separate from Model concerns which are kept separate from Controller concerns. If you follow those conventions, you'll get more milage out of the framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜