0

I am currently working on a Grails project and would like to know the best way of showing a select list of objects from the Spring Security domain model:

I know how to create a select list but I just want to find out the best way within grails to populate it with the objects from that domain. I was thinking of calling something like "user.list(params)" and then passing that to the view, I could then access the defined object within that domain, but im not 100% sure wether that is the best way or if it can be done that way?

Thanks in advance

EDIT.....

I have the following custom model setup in the controller:

class UserModel {
           String username
           String firstName
           String lastName

           def email = User.email.list()
           String[] emails = email

           static constraints = {
                username blank: false
                firstName blank: false
                lastName blank: false

           }
}

Then when the index page for this view is initialized I pass this to it:

[model: new UserModel(copy)]

and finally on the view i have this:

<g:select name="emails" from="${model.emails}"

Now when I run the application I get this error:

No signature of method: grails.plugins.springsecurity.ui.UserModel.propertyMissing() is applicable for argument types: () values: [] Possible solutions: propertyMissing(java.lang.String)

Can anyone please help with this?? Thanks

user723858
  • 1,017
  • 3
  • 23
  • 45

2 Answers2

0

You approach looks absolutely correct. In the controller action you populate the model:

def listUsers = {
    [ 'users': Users.list( params ) ]
}

and in the view you use the model for populating the listbox:

<g:select name="users" from="${ users }" />

Your issue looks very simple, so I hope, I've got the idea behind the question correctly :-)

Tom Metz
  • 919
  • 6
  • 7
  • I already have a model being passed to the view, I am presuming its not possible to pass another model to the view and only a mapped model which i dont have – user723858 Oct 09 '12 at 09:40
  • I need to add the list of say User E-Mails as a String[] in that custom model and then when the whole model is passed to the view i populate the slect list from the model.e-mails. How would I do this? – user723858 Oct 09 '12 at 09:56
0

I have been working hard at this and have implemented a solution for the issue I was having and it all works fine. Below is what I have in my controller for the view in question:

def index = {
        [userList: Users(), command: new RegisterCommand(copy)]
    }

protected List Users() {
        lookupUserClass().list()
    }

The List Users calls another function from an extended class which is shown below:

protected Class<?> lookupUserClass() {
        grailsApplication.getDomainClass(lookupUserClassName()).clazz
    }

This view then renders the list of items that I want to show in a select list:

<g:select name="emails" from="${userList.emails}" />

You can also do this if you are looking for unique values:

<g:select name="emails" from="${userList.emails.unique()}" />

Thanks

user723858
  • 1,017
  • 3
  • 23
  • 45