我确信这必须是编程中最常见的事情之一,即如何将某个表单提交与某个登录用户相关联。
我已经尝试了很长一段时间,但我最终还是在圈子里跑,一个接一个地出错。
我的问题是,让用户拥有他们提交的记录的正确方法是什么?
我尝试了很多选择,都没有成功。:\ 我什至尝试了创建字符串字段并将当前用户插入到 gsp 的隐藏字段中的不太安全的方法,这甚至无法正常工作。
静态的 belongsTo = 用户
在域类中,很好,但这只能让我在用户表单中看到它。
也许问题在于它是如何搭建的。Generate Controllers, Generate Views 为所有记录做了一个很好的通用列表。我需要这个列表来只显示他们的记录。但另外我需要我的管理员查看所有记录。
谁能帮我指出正确的方向?
SalesOrderController.groovy
@Secured(['IS_AUTHENTICATED_REMEMBERED'])
def list(Integer max) {
params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
[salesOrderInstanceList: SalesOrder.list(params), salesOrderInstanceTotal: SalesOrder.count()]
// def user = params.id ? SecUser.findByUsername(params.id) : SecUser.get(springSecurityService.principal.id)
// [salesOrderInstanceList: SalesOrder.findAllBySoldBy(user), salesOrderInstanceTotal: SalesOrder.count()]
// if (!user) {
// response.sendError(404)
// return
// }
}
类销售订单{
static searchable = {
user(component:true)
}
enum JobStatus {
PENDING, APPROVED, COMPLETE
}
enum JobType {
INSTALL, REPAIR, WARRANTY, TINT
}
enum PaymentType {
INSURANCE, CASH
}
enum InstallLocation {
INSHOP, HOME, BUSINESS
}
enum InstallTime {
MORNING, MIDDAY, AFTERNOON
}
JobStatus jobStatus
JobType jobType
PaymentType paymentType
String custFirstName
String custLastName
String custBestNumber
String custAlternateNumber
String custEmail
String custAddress
String custAddress2
String custCity
String custState
String custZip
String vehicleYear
String vehicleMake
String vehicleModel
String vehicleVin
static hasMany = [glassTypes: GlassType, options: GlassOption]
Date insuranceDateOfLoss
String insuranceCompany
String insurancePolicyNumber
String insuranceClaimNumber
String insuranceDeductible
Date installDate
InstallTime installTime
InstallLocation installLocation
String installCrossStreet1
String installCrossStreet2
String installAddress
String installCity
String notes
Date dateCreated
String soldBy
static constraints = {
jobType blank: false
custFirstName blank: false
custLastName blank: false
custBestNumber blank: false, size:10..10
custAlternateNumber nullable: true, sizeMax: 10
custEmail blank: false, email: true
custAddress blank: false
custAddress2 nullable: true
custCity blank: false
custState blank: false, size: 2..2
custZip blank:false, size: 5..5
vehicleYear blank: false, size:4..4
vehicleMake blank: false
vehicleModel blank: false
vehicleVin blank: false, size:17..17
glassTypes blank: false
options blank: false
insuranceDateOfLoss nullable: true
insuranceCompany nullable: true
insurancePolicyNumber nullable: true
insuranceClaimNumber nullable: true
insuranceDeductible nullable: true
installDate blank: false
installTime blank: false
installLocation blank: false
installCrossStreet1 blank: false
installCrossStreet2 blank: false
installAddress nullable: true
installCity nullable: true
paymentType blank: false
jobStatus blank: false
notes nullable: true, size:0..1024
soldBy blank: false
}
static belongsTo = SecUser
}
<div class="fieldcontain ${hasErrors(bean: salesOrderInstance, field: 'soldBy', 'error')} required">
<label for="soldBy">
<g:message code="salesOrder.soldBy.label" default="Sold By" />
</label>
<g:textField name="soldBy" value="${salesOrderInstance?.soldBy}" default="${sec.loggedInUserInfo(field:'username')}" />
<sec:loggedInUserInfo field="username"></sec:loggedInUserInfo>
我尝试了一个使用服务的示例,但这只是让我迷失了,因为该示例只需要传递一个字段。正如你所看到的,我有不止一个,还有一些多对一的加入。给我留下的问题多于答案。