Jordan Faust
Jordan Faust

Reputation: 103

Grails select not returning the object but a string

How can I get a select tag to return the object itself and not a string? Whenever I go to create a new BusinessArea I get an error that says:

"Failed to convert property value of type java.lang.String to required type bassscheduler.BusinessType for property businessType; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [bassscheduler.BusinessType] for property businessType: no matching editors or conversion strategy found"

Am I doing something wrong? I cant seem to find anything on how to get the select tag to return the object itself and not a string

Thanks for the help

I have the following model

class BusinessArea {

  BusinessType businessType
  // The date and time this occurence was created
  Date dateCreated
  // The date and time that this occurrence was last maintained.
  Date lastUpdated
  // The identifier for the OPERATOR or PROGRAM that last maintained this occurrence.
  int lastMaintainer
  // The name of the business area
  String name

  static constraints = {
    lastMaintainer blank: false, nullable: false
    name blank: false, nullable: false
  }
}

and controller action

def createBusinessArea() {

  def businessArea = new BusinessArea(params)
  if (!businessArea.save(flush: true)) {
    render view: "index", model : [businessArea: businessArea, activeTemplate: "businessArea"]
    return 
  }
  redirect controller: "admin", action: "index", model : [activeTemplate: "businessArea"]
}

With this form submitting to the controller action

<g:form controller="admin" action="createBusinessArea"> 
  <div class="row">
    <legend>Business Area</legend>
<div class="span3">
  <label>Business Area:</label>
  <g:textField name="businessArea"/>
</div>
<div class="span3">
  <label>Business Type:</label>
  <g:select name="businessType" from="${businessTypes}" optionValue="name"/>
</div>
  </div>

  <div class="buttons">
    <div class="row">
      <div class="span7" style="padding-top: 2em">
    <g:submitButton class="btn btn-primary pull-right" name="create" value="Create" />
  </div>
    </div>
  </div>
</g:form>

Upvotes: 2

Views: 1432

Answers (1)

Gregg
Gregg

Reputation: 35864

This:

<g:select name="businessType" from="${businessTypes}" optionValue="name"/>

should be:

<g:select name="businessType.id" from="${businessTypes}" optionValue="name" optionKey="id" />

Upvotes: 2

Related Questions