-1

我已经像这样创建了多对一关系 N:1

class Score {
    Double value
    Criteria criteria
    static belongTo = [criterias: Criteria]
}

class Criteria {
    String name
}

所以我的问题是我想用来g:select将某些标准名称的分数存储到数据库中。我有这个表格

   <g:form controller="audition" action="assignScore">

    <label for="vocal" class="control-label">vocal</label>
    <div class="controls">
        <g:select  from="${['0','10','20','30','40','50','60','70','80','90','100']}" name="vocal" value="score"   noSelection="['':'-choose Score please-']"/>
        <span class="help-inline">${hasErrors(bean: contestantInstance, field: 'vocal', 'error')}</span>

</div>

    <label for="stage" class="control-label">stage</label>
    <div class="controls">
        <g:select  from="${['0','10','20','30','40','50','60','70','80','90','100']}" name="stage" value="score"   noSelection="['':'-choose Score please-']"/>
        <span class="help-inline">${hasErrors(bean: contestantInstance, field: 'stage', 'error')}</span>

</div>
       <div class="form-actions">
           <g:submitButton name="save" class="btn btn-primary" value="save" />
           <button class="btn" type="reset">Cancel</button>
       </div>

   </g:form>

和控制器中的动作

 def assignScore = {
    //find a criteria  name
  def criteriaInstance =  Criteria.findByNameLike(params.vocal)  ?: new Criteria(name: "vocal").save(flush: true)
    if (params.vocal == "vocal"){
    def score = new Score()

        score.properties=params.vocal


        criteriaInstance.addToScores(score)
        criteriaInstance.save(flush: true)
    }

    def criteriaInstanceStage =  Criteria.findByNameLike(params.stage)  ?: new Criteria(name: "stage").save(flush: true)

    if (params.stage == "stage"){
        def score = new Score()
        score.properties=params.stage

        criteriaInstanceStage.addToScores(score)
        criteriaInstanceStage.save(flush: true)
    }


}

问题是我无法保存与某些标准相关的分数

4

1 回答 1

1

请参阅文档,您可以毫无问题地使用 g:select。

<g:select name="criteria"
          from="${Criteria.list()}"
          value="${criteria}"
 />

请注意,Criteria 可能是保留字。

于 2013-03-18T16:55:40.870 回答