开发者

Moving Grails logic from view to controller

开发者 https://www.devze.com 2023-03-29 02:19 出处:网络
This question is a continuation of the question here. The code below is from one of my views and it works fine, but I am having difficulty moving this code to a controller.If my packages and classes

This question is a continuation of the question here.

The code below is from one of my views and it works fine, but I am having difficulty moving this code to a controller. If my packages and classes are not clear from the code below, look at the 开发者_开发技巧previous question to see how they are arranged.

<g:set var="roleAdmin" value='${org.me.example.userrole.Role.findByAuthority("ROLE_ADMIN")}' />
<g:set var="roleOfficeProfessional" value='${org.me.example.userrole.Role.findByAuthority("ROLE_OFFICE_PROFESSIONAL")}' />
<g:set var="roleDriver" value='${org.me.example.userrole.Role.findByAuthority("ROLE_DRIVER")}' />
<g:select name="loggedBy.id" from="${org.me.example.userrole.UserRole.findAllByRoleInList([roleAdmin, roleOfficeProfessional, roleDriver]).user}" optionKey="id" value="${loadInstance?.loggedBy?.id}"  />   

It may be a basic misunderstanding on my part, but I cannot get ANY of the methods that I have made in my controllers to work in my views, only the methods that Grails has created (i.e. list, create, delete, edit, etc.) will work. Any assistance / recommendations would be helpful.


The from and value attributes don't come from methods/actions in your controller. They come from, generally, the request object. I gave you the code in your other thread to do this correctly, but here it is again, slightly more detailed...

class SomeController {

   def show = {
     def loadInstance = whateverYouGetItFrom
     def users = UserRole.findAllByRoleInList([roleAdmin, roleOfficeProfessional, roleDriver]).user
     [loadInstance:loadInstance,users:users]
   }
}

And then you would have a show.gsp which contains

<g:select name="loggedBy.id" from="${users}" optionKey="id" value="${loadInstance?.loggedBy?.id}"  />

If I'm misunderstanding your question, please give us some more details.

0

精彩评论

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