我是Grails& Groovy的。我正在尝试从两个域类创建单个表单,但它不会更新这两个域。
1)我的域类是:a)
class Alacat { String email String name Date startdate = new Date() Date
enddate = new Date() String starttime String endtime boolean roadclose
static belongsTo = [user : User]
String toString() { return "User of $name (id: $id)" }
String getDisplayString() { return name }
static constraints = {
email email: true, blank: false
name blank: false
startdate blank: false
enddate blank: false
starttime nullable: true
endtime nullable: true
roadclose blank: false
alertuser nullable: false
}
}
(b)中
class User {
String loginId
Set alerts=[]
static hasMany = [ alerts : Alacat ]
static constraints = {
loginId size: 3..20, blank: false
}
}
2)我的Controller(UserController)逻辑是:
def createalert() {
if (request.method == "POST") {
def user = new User(params)
if (user.validate()) {
user.save()
flash.message = "Successfully Created User"
redirect(uri : '/User/createalert')
} else {
flash.message = "Error Creating User"
return [ user : user]
}
}
}
3)createalert.gsp视图类:
<g:form action="createalert">
<fieldset class="form">
<div class="fieldcontain required">
<label for="loginId">Login ID</label>
<g:textField name="loginId" value="${user?.loginId}"/>
</div>
<div class="fieldcontain required">
<label for="alerts.email"> Email address:</label>
<g:textField name="alerts.email"
value="${user?.alerts?.email}"/>
</div>
<div class="fieldcontain required">
<label for="alerts.name">Name & Title:</label>
<g:textField name="alerts.name"
value="${user?.alerts?.name}"/>
</div>
<div class="fieldcontain required">
<label for="alerts.startdate">Start Date Notice:</label>
<g:datePicker name="alerts.startdate" value="${new Date()
}" precision="day" />
</div>
<div class="fieldcontain required">
<label for="alerts.enddate">End Date Notice:</label>
<g:datePicker name="alerts.enddate" value="${new Date() }"
precision="day" />
</div>
<div class="fieldcontain required">
<label for="alerts.starttime">(optional) St Time:</label>
<g:textField name="alerts.starttime"
value="${user?.alerts?.starttime}"/>
</div>
<div class="fieldcontain required">
<label for="alerts.endtime">(optional) E-Time:</label>
<g:textField name="alerts.endtime"
value="${user?.alerts?.endtime}"/>
</div>
<div class="fieldcontain required">
<label for="roadclose">closed 24-hours?</label>
<g:radioGroup name="roadclose"
labels = "[ 'Yes', 'No']"
values="[ 'Y', 'N' ]"
value = "Y">
${it.label} ${it.radio}
</g:radioGroup>
</div>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="createalert" value="Create Alert"/>
<g:link action="createalert">Back to Create Information </g:link>
</fieldset>
注意:我的问题是,只有Alacat没有任何内容时才会创建用户域(User-loginId)信息。我希望使用createalert表单创建来自两个域(User&amp; Alacat)的属性。运行时我没有收到任何错误。
答案 0 :(得分:0)
params
地图可能看起来像这样:
[
loginId: 'something',
alerts: [
email: 'blah',
name: 'blah blah',
...
]
]
所以正在实例化User对象:
def user = new User(loginId: 'something', alerts: alerts)
User.alerts
是一个集合,而不是地图。此外,我相信你需要调用instance.addTo*()
来添加到hasMany Collection。试试这个:
def user = new User(params)
user.addToAlerts new Alacat(params.alerts)
...
创建用户 param
会更清晰<g:textField name="user.loginId" value="${user?.loginId}"/>
然后执行此操作:
def user = new User(params.user)
斯坦的回答:def createalert() {
if (request.method == "POST") {
def User user = new User(params)
if (user.validate()) {
user.addToAlerts new Alacat(params.alerts)
user.save()
flash.message = "Successfully Created User"
} else {
flash.message = "Error Creating User"
}
}
}
}
答案 1 :(得分:0)
看起来像这样:
<span>
另外,我更改了用户域约束,如下所示(即从loginId blank:false到loginId nullable:true),并且在User表的login_Id字段上写为null。
<div class="fieldcontain required">
<label for="loginId">Login ID</label>
<g:textField name="user.loginId" value="${user?.loginId}"/>
</div>
}
非常感谢您的快速回复。
答案 2 :(得分:0)
loginId字段为null,因为UserController信息应该是新用户(params.user)而不是新用户(params)。我的意思是问题出在我的UserController上。
非常感谢Emmanuel的帮助。我真的很感激。