Grails创建多对一的belongsTo [英] Grails form creation for many-to-one belongsTo

查看:210
本文介绍了Grails创建多对一的belongsTo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 grails 的初学者,我有一个基本问题。我想在创建子类表单时显示父类的实例列表。



My Domain类如下。

  class Company {
字符串名称
静态约束条件= {


$ name

$ b $

$ b

My Child Class是公司的位置。

  class位置{
字符串名称
字符串地址
static belongsTo = {companyLocation:Company}

static constraints = {
name(blank:false)
address blank:false


String toString(){company:+ companyLocation +Location:+ name}
}

现在在位置视图的 _form模板中,我有 companyLocation下拉菜单的代码

 < div class =fieldcontain $ {hasErrors(bean:locationInstance,field:'companyLocation','error')} required> 
< label for =companyLocation>
< span class =required-indicator> *< / span>
< / label>
< / div>

当我进入创建页面时,我收到错误消息:

 错误500:内部服务器错误
URI / first_project / location / create
类groovy.lang.MissingPropertyException
消息否property:companyLocation for class:first_project.Location

为什么当我有一个静态变量时出现这个错误位置域类中定义的 companyLocation 请问一些请让我知道我哪里出了问题?



感谢您提前致谢。

解决方案

  static belongsTo = {companyLocation:Company} 

实际上应该是 $ b

  static belongsTo = [companyLocation:Company] 


I am a beginner in grails and I have a basic question. I want to display a list of instances of a parent class in the child class form creation.

My Domain class are as follows. The Parent class is the Company.

class Company {
    String name
    static constraints = {
        name(blank:false)
    }
    String toString(){name}
}

My Child Class is the Location of the company.

class Location {
    String name
    String address
    static belongsTo= {companyLocation:Company}

    static constraints = {
    name(blank: false)
    address blank:false 
}

    String toString(){"company:"+companyLocation+"Location:"+name}
}

Now in the _form template' of location view I have the code for thecompanyLocation dropdown`

<div class="fieldcontain ${hasErrors(bean: locationInstance, field: 'companyLocation', 'error')} required">
<label for="companyLocation">
<g:message code="location.companyLocation.label" default="companyLocation" />
    <span class="required-indicator">*</span>
    <g:select id="companyLocation" name="companyLocation.id" from="${first_project.Company.list()}" optionKey="id" required="" value="${locationInstance?.companyLocation?.id}" class="many-to-one"/>
</label>
</div>

When I go to the the create page I get the error:

Error 500: Internal Server Error
URI /first_project/location/create
Class  groovy.lang.MissingPropertyException
Message No such property: companyLocation for class: first_project.Location

Why am I getting this error when I have a static variable companyLocation defined in the Location Domain class? Could some please let me know where I have gone wrong?

Thanks in advance.

解决方案

This looks like a syntax issue,

static belongsTo= {companyLocation:Company}

should really be

static belongsTo= [companyLocation:Company]

这篇关于Grails创建多对一的belongsTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆